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.

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

5 Comments

Does that work with span text and python cause I can't get it to.
I haven't tried it. Could you link an example website that you are using?
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 allrecipes.com/search/results/?wt=Soup and retrieve those recipes? Is the goal to save all the links to the Soup recipes?
no, im just trying to find the title containing "soup" and then click on it and open that page
Try this elems = browser.find_elements_by_xpath("//span[contains(text(), 'Soup')]")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.