Home > Back-end >  How to click an element using Selenium and Python
How to click an element using Selenium and Python

Time:07-29

I have a problem with clicking an element using XPath in selenium. Here is the HTML element for the problem :

<label for="file"  style="display: inline-block;margin: 5px 10px;">Select File</label>

Do you know the solution for this? Any response is really appreciated.

CodePudding user response:

Can try with //label[@for='file'] or //label[@for='file'][text()='Select File']

If this is not working maybe you are targeting the wrong element or you are not waiting enough before it appears. When dealing with uploading files, I target the input with type file //input[@type='file'], not the label, you may need to provide bigger part of your HTML.

CodePudding user response:

if class=pb default is a unique class in the web-page then try this out.

driver.driver.find_element(By.XPATH,'//*[@]').click()

or you can search by text Select File

driver.find_element(By.XPATH,'//*[contains(text(),"Select File")]').click()

CodePudding user response:

Well No, you don't click on a <label> element. However, you may require to locate the <label> element for other purposes.


To identify the <label> element you can use either of the following Locator Strategies:

  • Using css_selector:

    element = driver.find_element(By.CSS_SELECTOR, "label.pb.default[for='file']")
    
  • Using xpath:

    element = driver.find_element(By.XPATH, "//label[@class='pb default' and text()='Select File']")
    
  • Related