Cannot find JavaScript rendered element with Selenium - python

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!

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 an element in a span class using Python Selenium

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();

Python Selenium upload file to website by class name

I am trying to use Python to upload files to a website. But when I looked at the Developer Inspection Tool, there is no id, and no name. I am trying to use class name, but it does not work.
Inspect window from Selenium:
Inspect window from website:
I've tried methods like these:
upload = driver.find_element_by_class_name("icomoon icon-upload2 toolbar-button fileinput-button")
upload = driver.find_element_by_class_name("fileupload")
upload = driver.find_element_by_class_name("btnContainer").
All failed.
the error message:
NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":".btnContainer"}
(Session info: chrome=91.0.4472.77)
Then I use:
upload = driver.find_element_by_css_selector('body > div.fileupload')
upload.send_keys(r'c\user\xxx\upload\excel.xlsx')
#or
upload = driver.find_element_by_xpath("//div[#class='fileupload']")
upload.send_keys(r'c\user\xxx\upload\excel.xlsx')
The error message:
ElementNotInteractableException: Message: element not interactable
(Session info: chrome=91.0.4472.77)
Can anyone help me with this?
Thanks,
JH.
If you want a quick solution you can always try getting by ccs selctor. Right click on the element in the inspect widow. click on copy > selector. And then in python write driver.find_element_by_css_selector('paste the selector here')
This is not an elegant solution but it should work.

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.

Error Message: no such element: Unable to locate element. How to rectify it?

\Some more Codes
for url in tqdmn(mel.Url[:10], leave=False):
driver.get(url)
Url_With_Coordinates.append(driver.find_element_by_css_selector('div[class="ugiz4pqJLAG__primary-text gm2-body-2"]'))
Here is the error message:
no such element: Unable to locate element: {"method":"css selector","selector":"div[class="ugiz4pqJLAG__primary-text gm2-body-2"]"}
HTML Element:
<div jstcache="243" class="ugiz4pqJLAG__primary-text gm2-body-2" jsan="7.ugiz4pqJLAG__primary-text,7.gm2-body-2">Example, 431 St Michale Rd, New York 3004, United States</div>
Please use the below line as ugiz4pqJLAG__primary-text and gm2-body-2 are 2 classes and you should always replace white space with . when you have multiple classes.
Url_With_Coordinates.append(driver.find_element_by_css_selector('div[class="ugiz4pqJLAG__primary-text.gm2-body-2"]'))

Categories

Resources