1

Test have multiple clicks which redirect to other page so I added some explicit waits there but when test is executed Selenium ignores one of two waits and performs click. And only then explicit wait performs when it already next page loaded and of course raises TimeoutException because it unable to locate element there. Also I need to mention that sometimes test executes properly but I think it's because the page has loaded in time.

def test_two(browser):
    browser.get('http://localhost/index.php?route=common/home&language=en-gb')
    macbook = browser.find_element(By.CSS_SELECTOR, "#content > div.row > div:nth-child(1) > form > div > div.content > div.button-group > button:nth-child(3)")
    iphone = browser.find_element(By.CSS_SELECTOR, "#content > div.row > div:nth-child(2) > form > div > div.content > div.button-group > button:nth-child(3)")

    browser.execute_script("arguments[0].click();", macbook)
    browser.execute_script("arguments[0].click();", iphone)
    browser.get('http://localhost/index.php?route=product/compare&language=en-gb')

    delete_macbook = WebDriverWait(browser, 3).until(EC.presence_of_element_located((By.CSS_SELECTOR, "#content > table > tbody:nth-child(7) > tr > td:nth-child(2) > form > a")))
    browser.execute_script("arguments[0].click();", delete_macbook)
    delete_iphone = WebDriverWait(browser, 3).until(EC.presence_of_element_located((By.LINK_TEXT, "Remove")))
    browser.execute_script("arguments[0].click();", delete_iphone)

I also tried to run this test in Firefox (initially it was Chrome) and it met each explicit wait correctly. So can it be browser or driver issue?

-written by newbie

fbx61588
  • 11
  • 3
  • chrome takes some time to load a new page. you should add a normal `time.sleep(3)` after you do your 2nd `browser.get(...` import time with `import time` at the top of your file – Dean Van Greunen Jul 28 '23 at 13:36
  • 1
    @DeanVanGreunen I'm trying to do this without time.sleep() – fbx61588 Jul 28 '23 at 13:54
  • 1
    See if changing from `EC.presence_of_element_located` to `EC.element_to_be_clickable` makes any difference. – Shawn Jul 28 '23 at 14:01
  • 1
    @Shawn No difference – fbx61588 Jul 28 '23 at 14:07
  • you should probably be using element.click() methods, and check expected condition of elementToBeClickable before... (if a standard page load happens after the click, Selenium will wait for the page to load automatically when click() method is used...) Right now you're using async JS calls to click those options/links. – pcalkins Jul 28 '23 at 16:22

1 Answers1

0

To click on any clickable element instead of presence_of_element_located() ideally you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

def test_two(browser):
    browser.get('http://localhost/index.php?route=common/home&language=en-gb')
    WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#content > div.row > div:nth-child(1) > form > div > div.content > div.button-group > button:nth-child(3)"))).click()
    WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#content > div.row > div:nth-child(2) > form > div > div.content > div.button-group > button:nth-child(3)"))).click()

    browser.get('http://localhost/index.php?route=product/compare&language=en-gb')
    WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#content > table > tbody:nth-child(7) > tr > td:nth-child(2) > form > a"))).click()
    WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Remove"))).click()
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352