how to check if a string contains comma ?

122 views
Skip to first unread message

Majid Lotfi

unread,
Nov 16, 2017, 3:49:12 PM11/16/17
to Selenium Users

I want to check if a string contains comma , I did :

if driver.find_element(:xpath, '//*[@id="left"]/div/div[1]/div[3]/div[5]/div[2]').text()..include? ',')
    puts "Failed"
else
puts "Passed"
end


Thanks

Shawn McCarthy

unread,
Nov 16, 2017, 3:57:01 PM11/16/17
to Selenium Users
selenium can get the text for you, but your language would be what checks if that string contains a comma. It looks like you are using ruby which I don't know, but try googling "ruby check if string contains string".

Also, you might want a better locator... something with an id or class or something.

Titus Fortner

unread,
Nov 16, 2017, 6:40:46 PM11/16/17
to seleniu...@googlegroups.com
Yeah ideally you find the thing that might have a comma, then get its text, then use your language to see if it has a comma:

sample_text = driver.find_element(id: 'maybe_comma').text
puts sample_text.include?(',') ? "Failed" : "Passed"

If you actually want to locate an element based on text, then I'm going to point you to Watir, which uses the Ruby Selenium library, but provides a number of extra locator strategies so you don't have to deal with XPath.

puts browser.divs(text: /,/).any? ? "Failed" : "Passed"


Titus Fortner
Solution Architect | Sauce Labs
Core Contributor | Selenium & Watir

Selenium conversations on Slack:  http://seleniumhq.herokuapp.com/


--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-users+unsubscribe@googlegroups.com.
To post to this group, send email to selenium-users@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/1d083909-c29f-4a1f-9813-f7d75cc5425b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages