How can I click on Sign in button to login to the web page as I am not getting any valid id to click on it
Asked
Active
Viewed 386 times
-4
-
Please read why [a screenshot of code is a bad idea](https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors). Paste the code and properly format it instead. Also add your own code attempts. – JeffC Feb 27 '20 at 22:29
2 Answers
2
In the 'Locating Elements' section of the Selenium with Python docs we are given a list of methods for finding elements:
- find_element_by_id
- find_element_by_name
- find_element_by_xpath
- find_element_by_link_text
- find_element_by_partial_link_text
- find_element_by_tag_name
- find_element_by_class_name
- find_element_by_css_selector
The first attribute inside the button tag is the id, which is set to 'csfWidgets-loginscreen-10-signIn-button'.
You should almost always use any ID when locating an element.
Calling the find_element_by_id method with the id will return the button element. A click event can then be triggered by calling click() on the button.
from selenium import webdriver
# Instantiate driver and navigate to website
driver = webdriver.Firefox()
driver.get("http://www.your_url.com")
# Locate button and click
button = driver.find_element_by_id('csfWidgets-loginscreen-10-signIn-button')
button.click()
schwab09
- 51
- 4
-
Hi, answers with no explaination at all are considered as low quality, though are not forbidden. Please add at least one or two sentences like why you solution works or how you got there. In general high quality answers are also more likely to get rewarded. – mischva11 Feb 26 '20 at 13:36
-
@schwab09 I added a brief explanation to your answer. Feel free to remove or change it, if you want to make it "yours". – JeffC Feb 26 '20 at 21:10
0
The desired element is a dynamic element so to locate/click() on the element with text as Sign in 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(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[id^='csfWidgets-loginscreen'][data-test='ccfk-button'] div.button__content[id$='signIn-content']>span.button__text"))).click()Using
XPATH:WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[starts-with(@id, 'csfWidgets-loginscreen') and @data-test='ccfk-button']//div[@class='button__content' and contains(@id, 'signIn-content')]/span[text()='Sign in']"))).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
undetected Selenium
- 183,867
- 41
- 278
- 352
-
Is there something wrong with using one of the IDs on the elements? I don't understand why your locators need to be so complicated. – JeffC Feb 26 '20 at 21:11
-
@JeffC As usual you still fail to understand usecases but at the same time you tend to abuse the voting system by down voting the answers which you don't understand. Factually I have given up explaining you that you need to understand dynamic elements. Instead of abusing the system by asking question as comments can you ask a proper question? Stackoverflow volunteers will be happy to help you out. – undetected Selenium Feb 26 '20 at 21:17
-
Lots of handwaving with no real content or legit argument.... There's no reason to believe, given the HTML provided and logical thought, that the ID on the Sign in link wouldn't be unique (plus [HTML standards](https://html.spec.whatwg.org/multipage/dom.html#the-id-attribute)). So given the HTML provided, why did you need to make such a complicated locator? Downvoting a bad/confusing answer isn't abusing the system, it's what it's there for. – JeffC Feb 26 '20 at 22:11
