Home > front end >  Selecting button in Selenium python using web driver
Selecting button in Selenium python using web driver

Time:11-06

Can anyone please let me know how to correctly click on the button using Selenium webdriver?

I have the following html element I want to click:

<button type="button"  data-bind="click: $parent.handleSsoLogin.bind($parent)"> Sign In
                                                </button>

I am trying to use WebDriver with python but it doesn't find the element. Please advise how to address it?

from xml.dom.expatbuilder import InternalSubsetExtractor
from selenium.webdriver.common.by import By
import time
# imports parts of interest
from selenium import webdriver
# controlling the chrome browser
driver = webdriver.Chrome()
link=xxxxx


driver.get(link2)
# login = driver.find_element(By.LINK_TEXT,"Login")
time.sleep(10)
# login.click()

driver.find_element(By.ID,'CybotCookiebotDialogBodyLevelButtonLevelOptinAllowAll')

time.sleep(10)

login=driver.find_element(By.CSS_SELECTOR("<button type="button data-bind="click: $parent.handleSsoLogin.bind($parent)"> Sign In


So far tried different elements but it doesn't find it

CodePudding user response:

Here is a complete example of how you can go to login page and login, on terex parts (why you edited out the url, I don't know).

Assuming you have a working Selenium setup, you will also need the following imports:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time as t
[...]
wait = WebDriverWait(driver, 5)
url = 'https://parts.terex.com/'
driver.get(url)
t.sleep(3)
try:
    wait.until(EC.element_to_be_clickable((By.ID, "CybotCookiebotDialogBodyLevelButtonLevelOptinAllowAll"))).click()
    print('accepted cookies')
except Exception as e:
    print('no cookie button!')
t.sleep(4)
login_button = wait.until(EC.element_to_be_clickable((By.XPATH, '//button[@data-bind="click: $parent.handleSsoLogin.bind($parent)"]')))
login_button.click()
print('clicked login button')
t.sleep(5)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, '//iframe[@]')))
try:
    wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[class='call']"))).click()
    print('accepted cookies again')
except Exception as e:
    print('no cookie iframe and button!')
driver.switch_to.default_content()
user_email_field = wait.until(EC.element_to_be_clickable((By.XPATH, '//input[@id="idcs-signin-basic-signin-form-username"]')))
user_email_field.send_keys('parts_dealer_112')

password_field = wait.until(EC.element_to_be_clickable((By.XPATH, '//input[@placeholder="Password"]')))
password_field.send_keys('password112')
login_button = wait.until(EC.element_to_be_clickable((By.XPATH, '//oj-button[@id="idcs-signin-basic-signin-form-submit"]')))
login_button.click()
print('logged in unsuccessfully')

Selenium documentation can be found here: https://www.selenium.dev/documentation/

CodePudding user response:

I have managed to locate the element using Chrome Plugin Selectors Hub Selectors Hub Google Chrome. It allows quickly select Xpath for elements which then can be used with Xpath locator- makes life so much easier- highly recommend giving it a try if you are struggling.

login=driver.find_element(By.XPATH,"//button[@data-bind='click: $parent.handleSsoLogin.bind($parent)']").click()
  • Related