1

i have to click the input type="button" element of the first li, but i can not refer to the id of the li, since it changes every time i i open the website, this is what i tried

driver.find_elements_by_xpath('//input[data-componentname="gender"]').get(0).click()

and it gives me this error:

AttributeError: 'list' object has no attribute 'get'

if i remove the get part, this is the error:

AttributeError: 'list' object has no attribute 'click'

This is the code, like i said, i have to click the first input without referring to the id

<ul data-componentname="gender">
    
      <li id="78ece221-1b64-4124-8483-80168f21805f" class="">
        <input type="button">
        <span>Uomo</span>
      </li>
    
      <li id="2678a655-b610-41e0-8e7f-bad58fbcb1b3" class="">
        <input type="button">
        <span>Donna</span>
      </li>
    
  </ul>

3 Answers3

1

To click on the element Uomo you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "ul[data-componentname='gender'] li:nth-of-type(1) span"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//ul[@data-componentname='gender']//span[text()='Uomo']"))).click()
    
  • Note : You have to add the following imports :

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

Browser Snapshot:

Ummo

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

find_elements method return a list of web elements.
The first member of the list can be received by simple indexation i.e. list_object[0]
Also, since you want to get the first element matching the //input[data-componentname="gender"] locator you can simply use find_element method.
So, instead of

driver.find_elements_by_xpath('//input[data-componentname="gender"]').get(0).click()

You can use

driver.find_element_by_xpath('//input[data-componentname="gender"]').click()

Or

driver.find_elements_by_xpath('//input[data-componentname="gender"]')[0].click()

UPD
well, you tried to use a wrong locator.
This should work:

driver.find_element_by_xpath("//ul[@data-componentname='gender' ]//span[text()='Uomo']/../input").click()

You can also click the element containing the "Uomo" text itself as well

driver.find_element_by_xpath("//ul[@data-componentname='gender' ]//span[text()='Uomo']").click()
Prophet
  • 32,350
  • 22
  • 54
  • 79
  • i get this errors for the first: AttributeError: 'list' object has no attribute 'click' for the second: IndexError: list index out of range i may need to click the input type button that's inside the list instead of the list.. – ilikepythoncode Dec 29 '21 at 17:15
  • That means the list is empty. Please share all your code including the link to the page you are working on or the HTML of the entire page. – Prophet Dec 29 '21 at 17:17
  • https://www.nike.com/it/register this is the page, i need to click the button called Uomo the one below the country dropdown – ilikepythoncode Dec 29 '21 at 17:51
  • See my updated answer and let me know if now it's OK – Prophet Dec 29 '21 at 19:22
0

You can include the first li if you only want that. For example:

driver.find_element_by_xpath('//input[data-componentname="gender"]/li[1]')

otherwise if you want the list then you can do

driver.find_elements_by_css_selector('input[data-componentname="gender"] > li')

and do the .get() according to which button you want to click

prasunnyd
  • 1
  • 1