2

Hello everyone I'm learning selenium recently and as I bet is a classic newbie mistake I filled my code with time.sleep which made everything really slow. So I started to learn about webdriverWait. I took some sample code and I have tried multiple things but I still get the error of "what you clicked is blocked by this other thing" meaning that the library did not crash but it also did not do anything. Which must mean I am doing something wrong. This is an error I can avoid if use the time.sleep function.

I'm using expected conditions, BY and action chains alongside WebDriverWait, though in my last test I tried to not use Acton chains to lower the possibilities of why its failing.

I'm using a company site that is probably under NDA so I don't have a public example to show this on, I tried searching for "pages with cover openings" but I couldn't find any, so if you guys know of any that I can use to illustrate this I would also find that really helpful. What is happening is that the site loads with a "cover" animation and after a few seconds the animation goes away to reveal the Button I'm looking for.

Environment info:

  • Using Pycharm community latest version
  • Using Selenium 4.0.0b2.post1 (I also tried in 3.9 to no results)
  • Using ChromeDriver 89 as my google chrome version is 89 as well [Version 89.0.4389.114 for Chrome]

Here are my code snippets:

Attempt #1:

from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
import selenium.webdriver.support.expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
import selenium.webdriver.support.ui as ui

def test(driver, actions, delay):
    cart = "//header/div[1]/div[1]/a[2]/*[1]"
    cartxp = WebDriverWait(driver, delay).until(EC.visibility_of_element_located((By.XPATH, cart)))
    actions.move_to_element(cartxp).perform()
    cartclk = driver.find_element_by_xpath(cart).click()

Attempt #2: Using UI library

from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
import selenium.webdriver.support.expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
import selenium.webdriver.support.ui as ui

def test2(driver, delay, actions):
    cart = "//header/div[1]/div[1]/a[2]/*[1]"
    cartxp = ui.WebDriverWait(driver, delay).until(EC.visibility_of_element_located((By.XPATH, cart)))
    actions.move_to_element(cartxp).perform()
    cartclk = driver.find_element_by_xpath(cart).click()

Attempt #3: Not using Action chains

from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
import selenium.webdriver.support.expected_conditions as EC
from selenium.webdriver.common.by import By

def test3(driver, delay):
    wait = WebDriverWait(driver, delay)
    cart = "//header/div[1]/div[1]/a[2]/*[1]"
    button = wait.until(EC.element_to_be_clickable((By.XPATH, cart)))
    cartclk = driver.find_element_by_xpath(cart).click()

All of these have a main method that just has:

driver = webdriver.Chrome()
test3(driver, 30)
driver.get('Site that I cant reveal cuz of NDA')
action = ActionChains(driver)

The error I get is:

Message: element click intercepted: Element ... is not clickable at point (1183, 27). Other element would receive the click

Thanks in advance for any help!

Edit: After some comments changed my xpath to //a[@class='header__cart'] both the previous and this one leads to a single result If I use it on chrome inspect to look for the button, additionally it works as intended if I use it to actually click the button after using time.sleep() to wait out the animation

Additionally just in case tried using the try catch as they did on the suggested question. It also failed

Attempt 4: surrounding in a try-catch

def test4(driver, delay):
    cart = "//a[@class='header__cart']"
    try:
        my_elem = WebDriverWait(driver, delay).until(EC.visibility_of_element_located((By.XPATH, cart)))
        print
        "Page is ready!"
    except TimeoutException:
        print
        "Loading took too much time!"
    cart_btn = driver.find_element_by_xpath(cart)
    cart_btn.click()

Error: selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <a href="/cart" class="header__cart" data-items-in-cart="0" )="">... is not clickable at point (1200, 27). Other element would receive the click

To clarify what I meant earlier this works:

cart = "//a[@class='header__cart']"
time.sleep(20)
cartclk = driver.find_element_by_xpath(cart).click()
H.Lopez
  • 21
  • 2
  • avoid using those path based xpaths... if you absolutely have to (you don't) then "//header" is unnecessary and there's probably a tag after... Also you don't need the moveTo... the click will already do that. – pcalkins Apr 12 '21 at 17:44
  • Does this answer your question? [Wait until page is loaded with Selenium WebDriver for Python](https://stackoverflow.com/questions/26566799/wait-until-page-is-loaded-with-selenium-webdriver-for-python) – vitaliis Apr 12 '21 at 17:47
  • "//header/div[1]/div[1]/a[2]/*[1]" - too unstable. Also, your question is too general, – vitaliis Apr 12 '21 at 17:48
  • It is very easy to create your own xPath's in ChromeDevTools and test them. The right-click copy Xpath should be your last resort always – JD2775 Apr 12 '21 at 17:52
  • I changed the xpath to //a[@class='header__cart']. @vitaliis where should I specify? I said what my error was, how it used to work and now how it does not work. As for the other question suggested, he is using general webdriver which as that question says will do a general wait for the page to load, I'm already using WebdriverWait to wait for an element in the page to be clickable – H.Lopez Apr 12 '21 at 18:31
  • Also close whatever the overlapping element is or just use driver.execute_script() and click the element you want – Arundeep Chohan Apr 12 '21 at 18:38
  • @ArundeepChohan I'm trying to wait out the overlapping element because it goes away by itself, once it goes away the button I want is revealed. – H.Lopez Apr 12 '21 at 18:50
  • You'd have to webdriver wait for the other element to be not visible then and then grab your element. – Arundeep Chohan Apr 13 '21 at 04:45
  • you might find the answer posted here useful: https://stackoverflow.com/questions/66820416/random-errors-using-wait-for-element-clickable-method-in-selenium – pcalkins Apr 13 '21 at 19:43

0 Answers0