Reputation: 35
Can you tell me why selenium can't click a button. I tried xpath, id, class, text and nothing. i get info that there is no such element or sth like that but in firefox i can see that there's an item the name is the same. No idea whats wrong.
self.driver.execute_script("window.scrollTo(0, 3500)")
sleep(1)
#self.action.move_to_element(przycisk).click(sprawdz).perform()
self.driver.find_element_by_xpath("//button[@id='sprawdz']").click();
#self.driver.find_element_by_link_text("ok").click();
Upvotes: 0
Views: 1163
Reputation: 193348
To click on the element you can use either of the following Locator Strategies:
Using css_selector
:
self.driver.find_element_by_css_selector("button.btn.btn-large#sprawdz").click()
Using xpath
:
self.driver.find_element_by_xpath("//button[@class='btn btn-large' and @id='sprawdz']").click()
Ideally, to click on the element you need to induce WebDriverWait for the element_to_be_clickable()
and you can use either of the following Locator Strategies:
Using CSS_SELECTOR
:
WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn.btn-large#sprawdz"))).click()
Using XPATH
:
WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='btn btn-large' and @id='sprawdz']"))).click()
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
As a last resort you can use execute_script()
method as follows:
Using CSS_SELECTOR
:
driver.execute_script("arguments[0].click();", WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn.btn-large#sprawdz"))))
Using XPATH
:
driver.execute_script("arguments[0].click();", WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='btn btn-large' and @id='sprawdz']"))))
Upvotes: 1
Reputation: 13
try this
element = self.driver.find_element_by_xpath("//button[@id='sprawdz']")
self.driver.execute_script("arguments[0].click();", element)
Upvotes: 0