Reputation: 21
Below is the HTML of the button I am trying to click with various options but its not working:
<button data-ng-click="Question.setAnswer(button.value,button.attemptNext)" class="btn btn-sm btn-primary " type="button">No</button>
I tried the following
new WebDriverWait(driver, 0).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@class='btn btn-sm btn-primary' and @value='No']"))).click();
but its not working TIA
Here is the console information. i have been seeing the webdriver error before I added the line Only local connections are allowed. Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code. Jan 27, 2020 1:03:02 PM org.openqa.selenium.remote.ProtocolHandshake createSession INFO: Detected dialect: W3C WebDriverException occured
Upvotes: 0
Views: 69
Reputation: 193068
You seem to pretty close. The <button>
element doesn't have a value
attribute but have the innerText as No
Additionally, WebDriverWait for the duration of 0
isn't an ideal configuration and must be set to a positive value.
However, the desired element is a Angular element so to so to locate/interact with the element you have to induce WebDriverWait for the element_to_be_clickable()
and you can use either of the following solutions:
Using CSS_SELECTOR
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn.btn-sm.btn-primary[data-ng-click*='attemptNext']"))).click()
Using XPATH
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='btn btn-sm btn-primary ' and text()='No']"))).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
I don't see any such error from the stacktrace you have provided. As you are still unable to click on the element inducing WebDriverWait as an alternative you can use execute_script()
as follows:
cssSelector
:
element_css = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn.btn-sm.btn-primary[data-ng-click*='attemptNext']")))
driver.execute_script("arguments[0].click();", element_css)
xpath
:
element_xpath = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='btn btn-sm btn-primary ' and text()='No']")))
driver.execute_script("arguments[0].click();", element_xpath)
Upvotes: 0
Reputation: 50809
No
is the text, not the value. Use
new WebDriverWait(driver, 0).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@class='btn btn-sm btn-primary' and .='No']"))).click();
Upvotes: 1