Reputation: 3075
I am working with this code code:
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?
Upvotes: 0
Views: 870
Reputation: 104
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.
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
Upvotes: 1
Reputation: 193088
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()
Upvotes: 0