Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
106 views
in Technique[技术] by (71.8m points)

python - Selenium - Not able to find Button Element in HTML code

I have just started learning Selenium and was playing around a bit trying to automate a search on the secondhand clothing website vinted.fr.

I was able to find all necessary Elements in the HTML code, except one:
-The Button to change the "sort by" functionality to "newest". ( This is a Screenshot of the Page including the Button I want to press)


Now I am wondering, what the Problem might be:
-Is it possible, that the button is not having a "button" tag in the HTML code. If yes how do I find and click this element?
-Am I just searching at wrong position?


Here is the HTML source code where I suggest the button element to be in:

Screenshot of the HTML code

question from:https://stackoverflow.com/questions/65850032/selenium-not-able-to-find-button-element-in-html-code

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

To click on the element to set sort by functionality to newest 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.pile li:nth-of-type(4) label"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//ul[@class='pile']//li[@class='pile__element'][last()]//label"))).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
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...