0

I have a button search, and it will show "No results found" or "another button for buy".

What I'm trying to do is to create an if to check if there is on page "No results found", click on the back button. If not, click on the buy button.

Here is HTML code:

<button> Search </button>
..
<div class="contents"> <h2>No results found<h/2> </div> #the message I want to check first
..
<button class="btn-standard> Buy Now </button> #buy button
...
<button class="back"> Back </button> #the button I want to click if no resul

And I have tried something like below:

if driver.find_element_by_xpath('//h2[text()="No results found"]'):
   driver.find_element_by_class_name('back').click()
else:
   WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(., 'Buy Now')]"))).click()

But it didn't work. Do I have to use try / catch codition?

ANOTHER EDIT:

I got this error now. I have no idea how to hide that element which obscure my Buy button.

ElementClickInterceptedException: Message: Element <button class="btn-standard buyButton"> is not clickable at point (1086,641) because another element <div class="ut-click-shield showing interaction"> obscures it

Paul Vio
  • 57
  • 1
  • 7

4 Answers4

2

You can implement a method that checks if element exists

public boolean checkElementExists(By by) {
    try {
        driver.findElement(by);
    } catch (NoSuchElementException e) {
        return false;
    }
    return true;
}

Then you can put it inside if clause

if(!checkElementExists(By.xpath("//h2[text()="No results found"]")))
    click back
1

The element with text as No results found would appear only after a unsuccessful search. So to lookout for the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following based Locator Strategies:

try:
    WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='contents']//h2[text()='No results found']")))
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='back' and contains(,. 'Back')]"))).click()
except TimeoutException:
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='btn-standard' and contains(,. 'Buy Now')]"))).click()
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • and if the back button, contains no text, do I have to use the following code line?` `WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='back']"))).click()` ` – Paul Vio Dec 25 '19 at 16:35
  • @PaulVio Yes, you are right, but you have to ensure the locator strategy `(By.XPATH, "//button[@class='back']")` identifies the desired element uniquely else you have refer the ancestor elements as we did in case of `//div[@class='contents']//h2` – undetected Selenium Dec 25 '19 at 16:42
  • Yea, works, but one more thing, and I hope it's done.. Can you have another look on my latest edit? – Paul Vio Dec 25 '19 at 18:11
0

find_element_* will throw an exception if the element doesn't exists, use find_elements_* and check if the returned list is not empty

wait = WebDriverWait(driver, 20)
elements = driver.find_elements_by_xpath('//h2[text()="No results found"]')
if elements:
    wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'back'))).click()
else:
    wait.until(EC.element_to_be_clickable((By.XPATH, "//button[contains(., 'Buy Now')]"))).click()
Guy
  • 46,488
  • 10
  • 44
  • 88
  • I get this error now: `ElementClickInterceptedException: Message: Element – Paul Vio Dec 25 '19 at 12:19
0

You can get the list of the element and check if the size of the element is greater than 0 or not and then you can click on it.
You can do it like:

 results = driver.find_elements_by_xpath('//h2[text()="No results found"]')
 if(len(results)>0):
     driver.find_element_by_xpath("//button[contains(text(),'Back')]").click()
 else:
     driver.find_element_by_xpath("//button[contains(text(),'Buy Now')]").click()
Sameer Arora
  • 4,439
  • 3
  • 10
  • 20