Issue
I am currently dabbling in Python in combination with Selenium. I can't get any further at one point.
Enclosed you can see three screenshots. At https://www.easycredit.de I already click on the button (1). After that I get to the next page. I would like to click this button (2) now. In Screenshot 3 you can see the location in the source code.
# link to Chromedriver
browser = webdriver.Chrome('/usr/local/bin/chromedriver')
button = browser.find_element(By.CLASS_NAME, 'econ-button btn btn-primary')
button.click()
The error:
NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":".econ-button btn btn-primary"}
(Session info: chrome=104.0.5112.79)
Here are my questions:
- hy does it not work with this code?
- How do you find out with which procedure it works best in a case like this?
- How do you choose whether to identify an element by XPATH, ID etc.?
Thanks
Solution
econ-button btn btn-primary
are actually 3 class names.
By.CLASS_NAME
gets only single class name parameter.
To work with locators containing multiple class names you can use By.XPATH
or By.CSS_SELECTOR
.
As for me both the above methods are good, each of them having several cons and pros.
So, here you can use
browser.find_element(By.CSS_SELECTOR, 'button.econ-button.btn.btn-primary')
Or
browser.find_element(By.XPATH, "//button[@class='econ-button btn btn-primary']")
Generally, you can use By.CSS_SELECTOR
or By.XPATH
. No need to utilize By.ID
or By.CLASS_NAME
since they are actually internally immediately translated to By.CSS_SELECTOR
or By.XPATH
:)
Some people preferring to use By.CSS_SELECTOR
while others prefer By.XPATH
.
As I mentioned previously, each of the above 2 methods having cons and pros.
For example you can locate elements by their texts with XPath only. XPath supports locating parent element based on their child nodes.
On the other hand XPath will not work so good on Firefox driver while it works perfectly on Chrome driver etc.
UPD
The locator for the second nein
radio button can be:
"//label[.//input[@data-econ-property='kreditdaten-beduerfnisfragen-flexibilitaetGewuenscht'][@value='radio3']]"
So, Selenium click can be done with
browser.find_element(By.XPATH, "//label[.//input[@data-econ-property='kreditdaten-beduerfnisfragen-flexibilitaetGewuenscht'][@value='radio3']]").click()
And so on with other buttons
Answered By - Prophet
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.