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')
Related
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();
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")
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.
Python script:
userId_box =driver.find_element_by_id("userId")
userId_box.send_keys(usr)
Inspect element:
<ion-input class="passwordinput sc-ion-input-md-h sc-ion-input-md-s md hydrated" placeholder="User ID" id="userId" type="text" value=""><ion-icon class="eye md hydrated" role="img"></ion-icon><input class="native-input sc-ion-input-md" aria-labelledby="ion-input-0-lbl" autocapitalize="off" autocomplete="off" autocorrect="off" maxlength="3" name="ion-input-0" placeholder="User ID" type="text"></ion-input>
Error:
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
You can try explicit wait
Sometimes pausing the program also helps
Sometimes the element is not in view and you have to scroll to the element in order to interact with the element. You can also try that using JavascriptExecutor
I'm a beginner in Selenium. I want to type the address in the search box. But I have a problem with finding that field. Field HTML code:
<input type="text" class="SearchField_SearchField__hDobT" placeholder="Address..." value="">
I'm trying to do something like this:
element = driver.find_element_by_xpath("//input[#class='SearchField_SearchField__hDobT']")
or
element=driver.find_element_by_class_name("SearchField_SearchField__hDobT")
I get that errors:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//input[#class='SearchField_SearchField__hDobT']"}
(Session info: chrome=80.0.3987.132)
or
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".SearchField_SearchField__hDobT"}
(Session info: chrome=80.0.3987.132).
I cannot access other fields either. I render the HTML content with JavaScript.
I don't know what the problem is. Please help!