Home > Net >  FInd specific element in selenium python
FInd specific element in selenium python

Time:10-13

I'm trying to have my program that uses selenium find (and later click) on this html code:

<svg aria-label="Add Photo or Video" class="_8-yf5 " color="#262626" fill="#262626" height="24" role="img" viewBox="0 0 48 48" width="24"></svg>

What function would I use to find this specific element? Thanks

CodePudding user response:

Please use this xpath

yourEle = driver.find_element_by_xpath("(//svg[@aria-label='Add Photo or Video'])")

CodePudding user response:

That is a svg web element. You can locate (by xpath) like this :

//*[name()='svg' and @aria-label='Add Photo or Video']

There are basically 4 ways to click in Selenium.

I will use this xpath

//*[name()='svg' and @aria-label='Add Photo or Video']

Code trial 1 :

time.sleep(5)
driver.find_element_by_xpath("//*[name()='svg' and @aria-label='Add Photo or Video']").click()

Code trial 2 :

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[name()='svg' and @aria-label='Add Photo or Video']"))).click()

Code trial 3 :

time.sleep(5)
button = driver.find_element_by_xpath("//*[name()='svg' and @aria-label='Add Photo or Video']")
driver.execute_script("arguments[0].click();", button)

Code trial 4 :

time.sleep(5)
button = driver.find_element_by_xpath("//*[name()='svg' and @aria-label='Add Photo or Video']")
ActionChains(driver).move_to_element(button).click().perform()

Imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains

PS : Please check in the dev tools (Google chrome) if we have unique entry in HTML DOM or not.

Steps to check:

Press F12 in Chrome -> go to element section -> do a CTRL F -> then paste the xpath and see, if your desired element is getting highlighted with 1/1 matching node.

  • Related