I would like to use an if function that tells me whether a button has been clicked or not in my code given below, if yes then "a" should be output if no then "b".
element = driver.find_element(By.XPATH, '//*[contains(concat( " ", @class, " " ), concat( " ", "css-xf3ahq", " " ))][text()="Button"]')
element.click()
if element == True:
print("a")
else:
print("b")
up to now b has always been written although the button is clickable
CodePudding user response:
Selenium has: element_to_be_clickable() which may be a better solution to what you're trying to do. i.e.
from selenium.webdriver.support import expected_conditions as EC
try:
WebDriverWait(driver, 10).until(EC.element_to_be_clickable(By.XPATH, '//*[contains(concat( " ", @class, " " ), concat( " ", "css-xf3ahq", " " ))][text()="Button"]'))
print('a')
except selenium.common.exceptions.TimeoutException:
print('b')
CodePudding user response:
you can use element_to_be_clicked condition
like this:
element = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.ID, "myDynamicElement"))
)
CodePudding user response:
try:
element = driver.find_element(By.XPATH, '//*[contains(concat( " ", @class, " " ), concat( " ", "css-xf3ahq", " " ))][text()="Button"]')
element.click()
print('Clickable')
except:
print('Not clickable')
Just try to click on it otherwise print error if it isn't. You can check for presence with a webdriver and handle different errors with multiple excepts if you want. Not sure if you can do it with an if here.
