0

I use selenium by this for web scraping:

from selenium import webdriver
path_to_chromedriver = '/usr/lib/chromium-browser/chromedriver'
browser = webdriver.Chrome(executable_path=path_to_chromedriver)

browser.get(url)
element = browser.find_element_by_id('email')

Now I have a website with generates a lot of nested tables and seems to assign ids automatically (and probably not every time the same). One thing that is reliable is that the table I'm interested in has a cell

<td>My Content</td>

Is there something like the following?

browser.find_element_by_text("<td>", text="My Content")
Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
  • @JeffC [Should comments saying “google it.” be flagged?](https://meta.stackoverflow.com/questions/280426/should-comments-saying-google-it-be-flagged/280427#280427) and [Should we downvote duplicates?](https://meta.stackexchange.com/a/62824/158075) – Martin Thoma Aug 17 '17 at 15:04
  • I downvoted it because the tooltop states, "This question does not show any research effort; it is unclear or not useful (click again to undo)" and your question shows no research effort. You completely made up some a method `.find_element_by_text()` and cited no references at all. This is a very common question and there are many QAs on SO and throughout the web that you would have found if you'd have made a reasonable attempt to find one. That shows me that you made no effort to research it. If you did google it, what keywords did you use? You aren't new to the site with ~22k rep. – JeffC Aug 17 '17 at 15:15

1 Answers1

1

Yes you can find the element with text match as well :

Use contains() method of xpath:

browser.find_element_by_xpath("//td[contains(text(),'My Content')]")

It will locate the element where it matches the text (we can use it for partial match)

OR you can use text() method like :

browser.find_element_by_xpath("//td[text()='My Content']")

Here you have to pass the complete string to be match including spaces. else it won't work

NarendraR
  • 7,577
  • 10
  • 44
  • 82
  • I tried `browser.find_elements_by_xpath("//*[contains(text(), 'My Content')]")`. Could you please explain the following? (1) What does the `//` in the beginning mean? (2) What does the `*` mean? (3) Is there a documentation for xpath you can recommend? – Martin Thoma Aug 17 '17 at 13:31
  • 1
    Sure, 1. `//` used for relative path 2. * means all tags - with your xpath it will find any tag which have `My Content` text – NarendraR Aug 17 '17 at 13:34
  • 1
    There are lots of blog available you can get this https://selenium-by-arun.blogspot.in/2017/04/327-introduction-to-xpath.html – NarendraR Aug 17 '17 at 13:37
  • 1
    Nice (+1)! I think it fits well in your answer, so you should instead add it there. If you simply don't have the time and don't mind, I would add it to your answer. One more thing: After I have the ``, I want to get the table. I can, of course, do `el.find_element_by_xpath("../..")` which _should_ work. But it would be nicer if I could somehow get the table which it belongs to directly. Is that possible? – Martin Thoma Aug 17 '17 at 13:40
  • `//table[contains(.,'My Content')]` try this it will locate your table which have text `My Content` – NarendraR Aug 17 '17 at 13:42
  • But it will also locate any other table which happens to have this string anywhere, right? I think the `browser.find_elements_by_xpath("//*[contains(text(), 'My Content')]")` is more "secure" in that case. – Martin Thoma Aug 17 '17 at 13:47
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/152159/discussion-between-tuks-and-martin-thoma). – NarendraR Aug 17 '17 at 13:48