2

So the webpage has a button that after clicking will add an element to the webpage, in which I can't find using selenium

Some imaginary code as follows to explain the problem I experience:

from selenium import webdriver
d = webdriver.Chrome()
#Go to my target website
d.get("https://some_website_url") #ref1
#Okay now loading of the website is done. `d` will not be updated and this is the problem!!

#Click my target button and an element with id="SecretButton" is loaded.
d.find_element_by_css_selector("#secretlyupdatethewebpage").click()

#Find #SecretButton but to no avail. 
#It can be found in the html panel of Chrome Developer Tools
#but cannot be found in the webdriver `d`, as `d` won't be 
#updated after #ref1
d.find_element_by_css_selector("#SecretButton").click()

How can I find that #SecretButton?

Ken T
  • 2,255
  • 1
  • 23
  • 30
  • 1
    U can use an explicit wait on the visibility of the element - https://selenium-python.readthedocs.io/waits.html – Grasshopper Feb 29 '20 at 08:13
  • 1
    @Grasshopper In between the two clicks, I am actually on debug mode and I wait until the page has totally updated. It turns out that the #SecretButton is on another frame so that's why I can't find the element when the frame hasn't been switched. And yes, I should add `WebDriverWait` in between the two clicks during the actual run. – Ken T Mar 01 '20 at 10:19

1 Answers1

3

To find and invoke click() on the secret button you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using ID:

    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "SecretButton"))).click()
    
  • Using CSS_SELECTOR:

    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#SecretButton"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='SecretButton']"))).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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352