1

I'm trying to create a program that clicks on boxes that contain a certain word, however all of the boxes have other words around them.

For example the site has a bunch of recipes, however I just want the ones that contain the word "soup". So it needs to be able to click on text that say, "tomato soup, "yummy soup", "some other soup type soup", and so on.

I've found this line.

WebDriverWait(driver, 10).until(expected_conditions.element_to_be_clickable((By.XPATH, "//span[text()='Soup']"))).click()

which is great but only works if you put the exact text in it. Ex. WebDriverWait(driver, 10).until(expected_conditions.element_to_be_clickable((By.XPATH, "//span[text()='Tomato Soup']"))).click()

If anyone knows how to do a more loose find that would be a big help. Thank You.

JQadrad
  • 541
  • 2
  • 16
Dave
  • 57
  • 1
  • 1
  • 7

1 Answers1

1

If you are using XPath 2.0 you could use a regular expression so look for everything that contains Soup.

//*[matches(@id, '.*Soup.*')]

Maybe take a look at How to use regex in XPath "contains" function.

Update

browser = webdriver.Chrome()
browser.get("https://www.allrecipes.com/search/results/?wt=Soup&sort=re&page=10")
elems = browser.find_elements_by_xpath("//span[contains(text(), 'Soup')]")
for elem in elems[:2]:
    print(elem.text)
JQadrad
  • 541
  • 2
  • 16
  • Does that work with span text and python cause I can't get it to. – Dave May 13 '20 at 23:56
  • I haven't tried it. Could you link an example website that you are using? – JQadrad May 14 '20 at 00:02
  • So you are iterating through all the pages to find Soup? I noticed that there is dynamic loading too. Why don't you use the search function like https://www.allrecipes.com/search/results/?wt=Soup and retrieve those recipes? Is the goal to save all the links to the Soup recipes? – JQadrad May 14 '20 at 00:10
  • no, im just trying to find the title containing "soup" and then click on it and open that page – Dave May 14 '20 at 00:17
  • Try this `elems = browser.find_elements_by_xpath("//span[contains(text(), 'Soup')]")` – JQadrad May 14 '20 at 00:17