1

I am trying to click on a button if an element is found on the page. The element is on the page most of the time. 3 times work, and 1 time doesn't work. Here is my code:

elements = driver.find_elements_by_xpath("//h2[contains(text(),'No results found')]")
if (len(elements)>0):
    WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.CLASS_NAME, 'ut-navigation-button-control'))).click()
else:
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(., 'Buy Now')]"))).click()

And below is the error I get sometime:

ElementClickInterceptedException: Message: Element <button class="ut-navigation-button-control"> is not clickable at point (128,80) because another element <div class="ut-click-shield showing interaction"> obscures it
    try:
        WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(., 'Buy Now')]"))).click()
    except:
        WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.CLASS_NAME, 'ut-navigation-button-control'))).click()

It's working like this, but it take a lot of time during the except. Does anyone know how to make it to go quickly through except?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Paul Vio
  • 57
  • 1
  • 7

2 Answers2

1

You can perform JavaScriptExecutor click on the element as it directly performs the action on the div and is not affected by the position of the element on the page.
You can do it like:

button = driver.find_element_by_xpath("//button[contains(text(),'Back')]")
driver.execute_script("arguments[0].click();", button)
Sameer Arora
  • 4,439
  • 3
  • 10
  • 20
  • it doesn't work. It is doing nothing.. do you have another idea? – Paul Vio Dec 25 '19 at 14:19
  • @PaulVio you have added this code inside your `if` condition right? and it is showing some error or what? – Sameer Arora Dec 25 '19 at 14:21
  • Yes, I added it inside if condition. It's not showing any error, It's working but it's not doing what I expect. – Paul Vio Dec 25 '19 at 14:26
  • @PaulVio you have posted another question where i have answered, so i have picked the xpath from that answer and have edit my this answer. So please pick the new answer from here and try that and let me know if that works – Sameer Arora Dec 25 '19 at 14:29
  • I have to find element by class name, not by text:(. So the one you edited will not help me – Paul Vio Dec 25 '19 at 14:31
  • @PaulVio I have used text because there can be chances that there are multiple elements with the same classname and that might be the reason your code is not working fine. – Sameer Arora Dec 25 '19 at 14:32
1

The element with text as No results found would appear only after a unsuccessful search. On a successful search, to click on the desired element, you have to induce WebDriverWait for the element_to_be_clickable() and you can use the following based Locator Strategies:

try:
    # wait for the visibility of the element with text as "No results found"
    WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//h2[text()='No results found']")))
    # if the element with text as No results found, induce WebDriverWait for invisibilityOfElement obscuring the clickable element
    new WebDriverWait(driver, 20).until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector("//div[@class='ut-click-shield showing interaction']")));
    # once the invisibilityOfElement obscuring the clickable element is achieved, click on the desired element inducing WebDriverWait
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='ut-navigation-button-control']"))).click()
except TimeoutException:
    # if search for the element with text as "No results found" raises "TimeoutException" exception click on the element with text as "Buy Now" inducing WebDriverWait
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(,. 'Buy Now')]"))).click()
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I get this error...I have tried your code, but I get the next error every time..`ElementClickInterceptedException: Message: Element – Paul Vio Dec 25 '19 at 20:03
  • @PaulVio Checkout the updated answer and let me know the status. – undetected Selenium Dec 26 '19 at 06:50