Home > Software design >  How To Get Selenium to Click Sign In Button
How To Get Selenium to Click Sign In Button

Time:12-05

I'm trying to get selenium to log in to a page to scrape data. I can't get it to click the "Sign in" button. Inspecting the button HTML gives me:

<button  data-litms-control-urn="login-submit" type="submit" aria-label="Sign in">Sign in</button>

The online documentation for selenium has been very unhelpful. Does anyone know what I would use to have selenium identify and click the button? The latest unsuccessful try I executed was:

driver.find_element(By.NAME, "Sign in").click()

Link to page: https://www.linkedin.com/uas/login?session_redirect=https://www.linkedin.com/my-items/saved-jobs/?start=0&fromSignIn=true&trk=cold_join_sign_in

CodePudding user response:

You can use the following CSS_Selector:

driver.find_element(By.CSS_SELECTOR, "button[type='submit']").click()

Don't forget to wait for the element to become clickable.
This should be done with use of WebDriverWait expected_conditions
As following

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(driver, 20)

wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[type='submit']"))).click()

CodePudding user response:

To get Selenium to click the sign in button, you would need to locate the sign in button on the page and then use Selenium's click() method to click it.

Here is an example of how you could do this in Python:

from selenium import webdriver

driver = webdriver.Chrome()
driver.get("https://www.example.com")

# Locate the sign in button using its CSS selector
sign_in_button = driver.find_element_by_css_selector("#sign-in-button")

# Click the sign in button
sign_in_button.click()

In this example, the find_element_by_css_selector() method is used to locate the sign in button on the page by its CSS selector. The click() method is then called on the sign_in_button element to click it.

Keep in mind that the CSS selector used in this example is just an example, and you may need to adjust it depending on the HTML structure of the sign in button on your page. You can also use other methods to locate the sign in button, such as find_element_by_id(), find_element_by_name(), or find_element_by_class_name().

You can refer to the Selenium documentation for more information and examples on locating elements and using the click() method.

CodePudding user response:

Try this!

from selenium.webdriver import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By

url = "https://www.linkedin.com/uas/login?session_redirect=https://www.linkedin.com/my-items/saved-jobs/?start=0&fromSignIn=true&trk=cold_join_sign_in"

wait_5 = WebDriverWait(driver, 5)

wait_5.until(EC.element_to_be_clickable((By.XPATH, '/html/body/div/main/div[3]/div[1]/form/div[3]/button'))).click()

It would be better to use selenium-wire undetected browser with proxies!)

  • Related