1

Guys I need to know how to find a text with Selenium, this one for example:

Test 

I can get the text with the following code:

wait.until(EC.element_to_be_clickable((By.XPATH, '//*[text()="Test"]'))).text 

But I need to get a text in the following format:

"Key" "[email protected]" 

I need to be able to get the above text, remembering the email and password may be different depending on the case, so I would like to get the full value of the string from the .com of the string, since the email will always have a .com, so in this case , I need to be able to find the .com and after finding me return the full value of the string.

9
  • Can we have some sample HTML code ? would be much easy to figure out how to get the ` full value of the string from the .com` Commented Aug 28, 2021 at 3:50
  • also remember that "" is a text node, not HTML DOM node. also .text() could represent the node, but one can not parse that with Selenium Commented Aug 28, 2021 at 3:52
  • @cruisepandey I didn't put it because it's complicated to explain what I'm doing lol, it's easier for me to just explain the classroom, basically I'm getting a text that is in double quotes and I don't use selectors because the id or class are always dynamic, so I need to a way to get the text from a part of the text that will always have, which in this case is .com Commented Aug 28, 2021 at 4:01
  • there can be different tag that will help us to determine this dynamic nature, unfortunately you won't be able to write xpath while working with text nodes. Commented Aug 28, 2021 at 4:04
  • @cruisepandey I've already discovered the cause that makes the element dynamic, but it's a browser behavior, there's no way I can prevent it, at most prevent it a little, and it's not feasible, I need to get the text by injecting some javascript code then, since I don't it will be possible to use the .text() method Commented Aug 28, 2021 at 4:12

1 Answer 1

1

Use contains in xpath to find a text that has .com. You can also use ends-with. The xpath would be something like this:

 //*[contains(text(),'.com')] 

Or

 //*[ends-with(text(),'.com')] 
Sign up to request clarification or add additional context in comments.

4 Comments

Selenium can't find it, is it because my text is in quotes?
It will never locate the element, since .com is not a plain text instead it is a text node
@Dkns- Even if the text of element is in " " double quotes, I was able to extract complete 'text` using contains. I can show you an example if you want.
@pmadhu : I would like to know when you inspect //*[contains(text(),'.com')] in HTML DOM, and when we have a text node, were you able to see any matches ? It's highly unlikely, xpath 2.0 can do that, but Selenium does not have support for V2.0, it still usages V1.0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.