Home > Blockchain >  Start With in Selenium
Start With in Selenium

Time:01-23

I tried to take the text after the word 'Chapeau' in this web site : https://www.guidedeschampignons.com/produit/agaric-auguste/

I get there with XPATH :

Chapeau = Chapeaux.append((driver.find_element(By.XPATH,'/html/body/div[3]/div/div[2]/div/div/div/div/div/div/div[2]/div/div/div/div[2]/div/div/div/div/div[2]/p[3]')).text)

But I want to take the text with a Star With because in fonction of the mushroom type the line is not always at the same place.

I tried that :

Chapeau = Chapeaux.append((driver.find_element(By.XPATH,("//div[starts-with(., 'CHAPEAU')]"))).text)

and I tried 'br' and 'strong' in place of div in my code but I can't find the text.

Sorry it's a little bit hard for me. Thanks for your help.

CodePudding user response:

Try with

//body//div[@class='uncont']//div/p[strong[contains(text(), 'CHAPEAU')]]

This is a more flexible and robust selector.

Explanation: Get me the div element with class 'uncont' that has a div that contains a p element that contains a strong element with text 'CHAPEAU'.

CodePudding user response:

Try this XPath.

//*[contains(text(), 'CHAPEAU')]//preceding::p[1]//following::p

If this solves your issue, please upvote and accept the answer. If this works, and if you do not understand the xPath, happy to explain the xPath construct. Let me know.

  • Related