Unable to locate an element in a span class using Python Selenium - python

I have been struggling to locate an element in a span class. The element is a radio-checkmark button. Here is the html:
<span class="radio-container" for="searchType_2">
<input class="form-check-input" type="radio" name="searchType" id="searchType_2" value="cidade">
<span class="radio-checkmark">
::after
As the classes above are not unique, I tried the following:
dropdown_menu = self.driver.find_element_by_css_selector('[for="searchType_2"] .radio-checkmark')
When I do the inspection and search using the CSS selector above it works. It shows me as 1 of 1. But when I run the code, I get the following exception:
no such element: Unable to locate element: {"method":"css selector","selector":"[for="searchType_2"] .radio-checkmark"}
(Session info: chrome=92.0.4515.107
Thanks

The element you are trying to access is inside an iframe. You need to switch to the iframe before accessing any element inside it
driver.switch_to_frame(driver.find_element_by_xpath("//iframe[#class='cz-map-frame']"))
driver.find_element_by_xpath("//input[#id='searchType_2']//following::span[#class='radio-checkmark'][1]").click();

Related

Selenium can't set text

Here is form element on the page
<input class="step__input" valid="true" id="capture-first-name" data-capture-id="first-name" name="first-name" placeholder="First Name" type="text" minlength="2" autocomplete="given-name" maxlength="32" pattern="((?!(\s{2})).){2,32}" autocapitalize="on">
When I do this...
browser.find_element_by_xpath('capture-first-name').send_keys('Adam')
I get error
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"capture-first-name"}
(Session info: chrome=92.0.4515.159)
How can I fix this?
That's an id.
You should use
browser.find_element_by_id('capture-first-name').send_keys('Adam')
instead of by_xpath
In case you wanna stick with xpath
browser.find_element_by_xpath("//input[#id='capture-first-name']").send_keys('Adam')

unable to locate element : selenium in python

I'm a newbie in coding with python and selenium. I try to find an element in a web-page by its class name.
<input type="email" class="whsOnd zHQkBf" jsname="YPqjbf"
autocomplete="username" spellcheck="false" tabindex="0"
aria-label="Adresse e-mail ou numéro de téléphone" name="identifier"
value="" autocapitalize="none" id="identifierId" dir="ltr"
data-initial-dir="ltr" data-initial-value="">
But each time I've tried I have this error.
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".whsOnd zHQkBf"}
I don't understand why because I verified and I think that it's not in an iframe so I don't understand why do I have this message and I don't know how to manage the error.
Here are the pictures of my code and the html :
My code
The HTML of the page
Hard to tell without seeing your code and the html. Are you sure it is not in a iframe?
If not you can perhaps try to use xpath to find the element.
When inspecting the webpage in chrome you can right click the element and say copy -> copy xpath. I do recommend looking into xpath, so you know what you are searching for.
Edit:
Try using only one class name, since find_element_by_class_name() only accepts one.
This:
driver.find_element_by_class_name("zHQkBf")
instead of this:
driver.find_element_by_class_name("whsOnd zHQkBf")

Selenium find_element_by_class_name and find_element_by_css_selector not working

I'm scraping a website that has the following structure:
<div class="cr__stores-list">
<div class="cr__stores-item">store1</div>
<a href="/stores/store1/">
<div class="cr__stores-item-b">
<div class="cr__container-name">
<h3 class="cr__container-name-title cr__text--subtitle3 cr__textColor--colorDark300">
Store Name
</h3>
</div>
</div>
</div>
<div class="cr__stores-item">store2</div>
...
...
<div class="cr__stores-item">store3</div>
<div class="cr__stores-item">store4</div>
</div>
Each "div class="cr__stores-item"" inside "div class="cr__stores-list"" has the exact same structure.
for each "div class="cr__stores-item"" I'm trying to scrape the url and the store name inside the h3 tag
I have the following code so far:
wd = webdriver.Chrome('chromedriver', options=options)
wd.get('stores_url')
element = WebDriverWait(wd, 20).until(EC.presence_of_element_located((By.CLASS_NAME, "cr__stores-list")))
list_stores_tags = element.find_elements_by_class_name("cr__stores-item")
for store_tag in list_stores_tags:
# store_tmp = store_tag.find_element_by_class_name("cr__container-name-title cr__text--subtitle3 cr__textColor--colorDark300")
# store_tmp = store_tag.find_element_by_css_selector('h3.cr__container-name-title cr__text--subtitle3 cr__textColor--colorDark300')
store= store_tmp.text
url_tmp = store_tag.find_element_by_tag_name("a")
url = url_tmp.get_attribute('href')
The 2 commented lines are my attempts at getting the store name.
With this code I'm successfully getting all urls of each store without problem.
However I'm unable to get the store names. Neither find_element_by_class_name nor find_element_by_css_selector seem to work as in both cases I get an exception:
Message: no such element: Unable to locate element: {"method":"css selector","selector":".cr__container-name-title cr__text--subtitle3 cr__textColor--colorDark300"}
(Session info: headless chrome=87.0.4280.88)
or
Message: no such element: Unable to locate element: {"method":"css selector","selector":"h3.cr__container-name-title cr__text--subtitle3 cr__textColor--colorDark300"}
(Session info: headless chrome=87.0.4280.88)
What am I doing wrong? Any suggestions would be welcome.
Thanks!
You're using find_element_by_css_selector incorrectly. Try
store_tag.find_element_by_css_selector('h3.cr__container-name-title.cr__text--subtitle.cr__textColor--colorDark300')
Note the dots instead of spaces.

Locate an fill input element with selenium python

I'm Trying to fill the next txtBox but I'have tried with the xpath, name, class.
<input name="txtNumDoc" type="text" id="txtNumDoc" class="txtBox">
But always get me the same error.
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[contains(.,'txtNumDoc')]"}
First, the element you are trying to access is inside an iframe so you need to switch to that first:
iframe = driver.find_element_by_id("iframeBDUA")
driver.switch_to.frame(iframe)
and then you can find your element:
element = driver.find_element_by_id("txtNumDoc")
//*[contains(.,'txtNumDoc')] is not a valid xpath for this element as we don't have text txtNumDoc for this element. txtNumDoc is attribute value for both name and id in this case.
use the below.
//input[#id="txtNumDoc"]

Find span class with selenium python

I am using selenium and python to learn about automation web testing.
I want to click on "Continue" button, while there is only span in it (I had learned that using id instead of span is so much easier)
but in this case, I want to click the span.
I am using below code:
driver.find_element_by_xpath("//span[contains(#class,'mdBtn01Txt')][contains(text(),'Continue')]").click()
here is the element :
<div class="mdLYR12Inner01">
<a class="MdBtn01 mdBtn01 ExDisabled FnSubmitBtn" style="display:none" href="#" onclick="charge(this); return false;">
<span class="mdBtn01Inner">
<span class="mdBtn01Txt">
Continue <<<(I want to click this button)
</span>
</span>
</a>
</div>
</footer>
But, I got this message below:
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//span[contains(#class,'mdBtn01Txt')][contains(text(),'Continue')]"}
Does not looks like a valid xpath, correct one should be
//span[#class='mdBtn01Txt']
Here is the rule to create xpath
Syntax for XPath:
XPath contains the path of the element situated at the web page. Standard syntax for creating XPath is:
Xpath=//tagname[#attribute='value']
Tagname: Tagname of the particular node.
#: Select attribute.
Attribute: Attribute name of the node.
Value: Value of the attribute.

Categories

Resources