0

I am working with this code code:enter image description here

I am trying to find text "Branch Selection" within a web page to click on it.

So I do this:

driver.find_elements_by_xpath("//*[contains(text(), 'Branch Selection')]").click()

It doesn't throw an error, but the click doesn't happen. What am I doing wrong?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
sprogissd
  • 2,755
  • 5
  • 24
  • 45

2 Answers2

1
  1. If it clicks but nothing happens, first try adding a sleep like sleep(5) to help debug before clicking to see if its because Selenium thought the page loaded when it actually didn't finish loading. If you can click after sleep of 5 seconds then you need to use WebDriverWait and EC as @DebanjanB had shown. In worst case scenario you'll have to use a sleep in your code but try to get the sleep to as short as possible.

  2. Otherwise you might have multiple elements on the page with Branch Selection text such as in the meta tags. Try using XPATH example below to isolate the XPATH look up:

"//button[.//*[contains(text(), 'Branch Selection')]]"

or if there's more than one phrase on page containing the text, use following to select exact text

"//button[.//*[text()='Branch Selection']"

This selects the button element that has a child element with the text you're looking for. More XPATH details here: https://devhints.io/xpath

j7an
  • 104
  • 2
  • 11
0

As per the HTML provided to click on the element with text as Branch Selection you need to induce WebDriverWait for the element to be clickable as follows :

  • CSS_SELECTOR :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    # lines of code
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.item.item-block.item-md div.input-wrapper>ion-label.label.label-md[id^=lbl-]"))).click()
    
  • XPATH :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    # lines of code
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='item item-block item-md']//div[@class='input-wrapper']/ion-label[@class='label label-md' and starts-with(@id,'lbl-') and contains(.,'Branch Selection')]"))).click()
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I am trying to use the suggested Xpath method, and I get a TimeoutException error. Does it mean the Xpath is wrong? – sprogissd Apr 23 '18 at 23:43
  • @sprogissd Possibly I might have wrongly typed some tag/attribute while copying from the _HTML_ snapshot. A text based formatted _HTML_ would have helped. But that another aspect can be that the `xpath` derived from the _HTML_ you provided may not be **unique** and we may require some more of the _outerHTML_ – undetected Selenium Apr 24 '18 at 06:14
  • @sprogissd A _TimeoutException_ error indicates that the element was not found. Can you check this discussion once? https://stackoverflow.com/questions/48471321/nosuchelementexeption-selenium-unable-to-locate-element/48472940#48472940 – undetected Selenium Apr 24 '18 at 15:47