0

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()

2 Answers 2

0

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()
5
  • Thanks for the answer. But like I said before. I leave the page already loaded and do the test, it still returns this error. I've already entered up to 60 seconds, and I still can't find it. Commented Jan 29 at 14:10
  • try using a while loop with WebDriverWait until its visible by handling the exception.Refer the original answer
    – naved196
    Commented Jan 29 at 14:59
  • see if the iframe contains the xpath
    – naved196
    Commented Jan 29 at 15:15
  • I tried this code, but apparently it doesn't work. I'm not sure, but I think the element is not in another iframe. Is there a way to search the page for all iframes? Commented Jan 29 at 18:43
  • switch to iframe's context and and locate the element within iframe. Refer the original answer.
    – naved196
    Commented Jan 30 at 2:23
0

I think that most of the time when an element is not found, it must be because of the iframe. That's what happened here. The friend helped me, not with the code, but with the idea of searching for iframes. Sorry if the question was redundant. Thank you @Naved196

Not the answer you're looking for? Browse other questions tagged or ask your own question.