Search code examples
python-3.xselenium-webdriverxpath

Elements not found by Selenium and Python, even though they are present


I'm trying to automate a repetitive process in building a website in the format of an e-commerce store (I'm an amateur). These are repetitive steps that would certainly save me an extraordinary amount of time. I've already put together some steps, but now I'm stuck trying to find a certain button, I inspect the browser and I can copy the xpath, but when I run the code it doesn't find it. I've read about 200 questions here and I still haven't been able to find an answer that helps me. So if anyone can help me I will be grateful.

Part of the error that appears is this.

TimeoutException Traceback (most recent call last) Cell In[62], line 221 218 WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//span[normalize-space()='Páginas']"))).click() 220 # Clique no botão 'Adicionar Páginas' --> 221 WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="app"]/div[1]/div[2]/div/div[1]/div[2]/div/div[2]/div/div/a/span'))).click()

TimeoutException: Message:

HTML do botão

<a href="https://admin.shopify.com/store/dc7020-3/pages/new" data-polaris-unstyled="true" class="Polaris-Button_r99lw Polaris-Button--pressable_1q8ey Polaris-Button--variantPrimary_1stsb Polaris-Button--sizeMedium_5f35p Polaris-Button--textAlignCenter_1kere"><span class="">Adicionar página</span></a>

Even though I leave the screen already loaded, and run the code just to click, it doesn't find it.

I've already tried: find_element; element_to_be_clickable; presence_of_all_elements_located

I've already tried: By.XPATH; By.ID; By.CSS_SELECTOR; By.NAME; etc

Part of the code I'm trying to run

 # Navigate to the "Pages" tab only the first time
        if i == 0:
            # Click on the 'Pages' tab
            WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//span[normalize-space()='Páginas']"))).click()

            # Click on the 'Add Pages' button
            WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="app"]/div[1]/div[2]/div/div[1]/div[2]/div/div[2]/div/div/a/span'))).click()

Solution

  • Here the Timeout Exception error indicates that the WebDriverWait method has been timed out before finding the element specified by the following XPath:

    '//*[@id="app"]/div[1]/div[2]/div/div[1]/div[2]/div/div[2]/div/div/a/span'

    which means it doesn't exist after 10 seconds.

    Try this by increasing the timeout period:

    WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="app"]/div[1]/div[2]/div/div[1]/div[2]/div/div[2]/div/div/a/span'))).click()
    

    Try using a while loop for WebDriverWait:

    loop_wait = WebDriverWait(driver, 20)
    
    while True:
    
      try:
    
        wbdrvr = loop_wait.until(EC.visibility_of_element_located((By.XPATH,'//*[@id="app"]/div[1]/div[2]/div/div[1]/div[2]/div/div[2]/div/div/a/span')))
      
        break
    
      except TimeoutException:
      
        pass
    
      wbdrvr.click()
    

    See if the iframe contains the XPath. Check here to know more about finding XPath using iframe.

    Switch to the iframe's context and locate the element within the iframe:

    driver.switch_to.frame(driver.find_element_by_id("iframe_id"))
    
    pg_tab = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[normalize-space()='Páginas']")))
    
    adpg_btn = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="app"]/div[1]/div[2]/div/div[1]/div[2]/div/div[2]/div/div/a/span')))
    
    pg_tab.click()
    
    adpg_btn.click()
    
    driver.switch_to.default_content()