Skip to content Skip to sidebar Skip to footer

Can Someone Please Explain Me How To Write An Xpath For Selenium On Python?

I'm having trouble clicking on elements in a web page. I'm trying to do this with: WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '..........']'))).click()

Solution 1:

Here you can find a good tutorial, which will teach you xPath basics. On the webpage when you press F12 the dev tools opens:

img

Then you click on one of the elemnts and then Ctrl+f. Then the search menu will open:

img2

There you can type xPaths and CSS Selectors to make sure, that you have found the right element. For example:

img3

once you have typed yor xPath , you will see on the right side the number of the elements found, also you can click on up and down buttons to iterate over every element, to see whic exactly element is on number 1,2...

Once you have found the right xPath, just copy it from dev tools and paste in the code like this:

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div"))).click()

In this case it will be selected only one element. If you want to wait until all elements, if there are more than 1, just use one of the EC methods, for example:

WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//div")))

this will wait for visibility of all elements located by //div xPath.

Hope this helps.

Post a Comment for "Can Someone Please Explain Me How To Write An Xpath For Selenium On Python?"