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

3 Answers 3

1

Key Changes:

  • Search for All <iframe> Elements

  • Make Your Xpath More Robust

  • Increase Wait Time a Little More

from selenium.common.exceptions import NoSuchElementException

# Make Your XPath More Robust
target_xpath = "//a[.//span[normalize-space()='Adicionar página']]"
# target_xpath = "//span[normalize-space()='Adicionar página']"

found = False

iframes = driver.find_elements(By.TAG_NAME, "iframe")

for idx, iframe in enumerate(iframes):
    driver.switch_to.default_content()  # Always reset first
    driver.switch_to.frame(iframe)
    # increase wait time
    try:
        btn = WebDriverWait(driver, 20).until(
            EC.element_to_be_clickable((By.XPATH, target_xpath))
        )
        print(f"Found inside iframe index: {idx}")
        btn.click()
        found = True
        break
    except:
        print(f" Not in iframe index: {idx}")
        continue

if not found:
    driver.switch_to.default_content()  # Reset frame
    print("Could not find the button in any iframe.")
Sign up to request clarification or add additional context in comments.

Comments

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 Comments

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.
try using a while loop with WebDriverWait until its visible by handling the exception.Refer the original answer
see if the iframe contains the xpath
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?
switch to iframe's context and and locate the element within iframe. Refer the original answer.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.