I am trying to find an input field by Id. The input field looks like
HTML:
<input id="hgz" onkeyup="runUpKey('form_hgzii);">
Python:
driver.find_element_by_id("hgz").send_keys("test")
Result:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="hgz"]"}
While iterating through all input fields
for inputs in driver.find_elements_by_tag_name('input'):
print(inputs.get_attribute("id"))
Python prints the correct ID, when im changing the code to
for inputs in driver.find_elements_by_tag_name('input'):
if inputs.get_attribute("id") == "hgz":
inputs.send_keys("test")
it also does not work.
Anyone an Idea?
Are you running this code right after opening the page? Maybe the DOM didn't finish loading. Try putting a sleep statement like sleep(5) before trying to get element by id
Related
Imgur Web Element
I want to write a line of code that clicks on the ellipsis on the webpage. I've attached the element description.
I can't share the webpage as it is password protected
Here's what I tried:
element = driver.find_element_by_css_selector('.fas.fa-ellipsis-v')
element.click()
I am expecting the ellipsis to pop open but i get the following error:
Message: no such element: Unable to locate element: {"method":"css selector","selector":".fas.fa-ellipsis-v"}
First of all, please, do not use images when your want a code review or help, it is more easier to help with raw code.
In that case:
Message: no such element: Unable to locate element: {"method":"css selector","selector":".fas.fa-ellipsis-v"}
Try to use this code below instead of driver.find_element_by_css_selector([...]) :
WebDriverWait(your_driver, your_timeout).until(EC.visibility_of_element_located((By.CLASS_NAME, your_locator)))
or
WebDriverWait(your_driver, your_timeout).until(EC.visibility_of_element_located((By.CSS_SELECTOR, your_locator)))
After the WaitDriverWait, use your find and click code.
If you need more help locating elements by, try look the Selenium Docs.
It worked by using the following line:
driver.execute_script("arguments[0].click();", element)
i have this error in selenium python
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[24]/div[1]/div/div/div[3]/div[1]/div/button[1]"}
i want the element with this xpath:
driver.find_element_by_xpath("/html/body/div[24]/div[1]/div/div/div[3]/div[1]/div/button[1]").click()
if doesn't exist, then click other element with other xpath like:
driver.find_element_by_xpath("/html/body/div[24]/div[1]/div/div/div[3]/div[1]/div/button[2]").click()
everything is clear if element 1 doesn't exists to click, click to element 2
how can I do this in selenium python
thanks
NoSuchElementException generally occurs because of one the below reasons
Element locator is incorrect
Element is not rendered
Page is not rendered or loaded
Element is inside a frame
You can have a look, do a debug and based on that write your code. :)
This is not the exact solution, but this is the idea
if elem1.is_displayed():
elem1.click()
else:
elem2.click()
I'd suggest you using time.sleep(10) and a nested try block to catch your errors while it accomplishes what you want to do. For example:
time.sleep(10)
try:
driver.find_element_by_xpath("/html/body/div[24]/div[1]/div/div/div[3]/div[1]/div/button[1]").click()
except:
print("button1 could not be found... Now trying to retreive button2")
try:
driver.find_element_by_xpath("/html/body/div[24]/div[1]/div/div/div[3]/div[1]/div/button[2]").click()
except:
print("button1 nor button2 were not found...")
Note that the time.sleep(10) delays your code from executing anything after this line for 10 seconds, and this allows the page to load properly for elements to be located easier.
Im trying to use a drop down list whit selenium :
browser.get('https://account.mail.ru/signup?from=main&rf=auth.mail.ru')
day = Select(browser.find_element_by_xpath('/html/body/div[1]/div[3]/div[3]/div/div/div/form/div[5]/div[2]/div/div[1]/div/div/select'))
dayDD=Select(day)
dayDD.select_by_index(3)
but i always get this error :
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[1]/div[3]/div[3]/div/div/div/form/div[5]/div[2]/div/div[1]/div/div/select"}
I dont know why it can't find xpath since i get it same way i get all my other xpaths
could you attach DOM page screen with list of elements?
At this stage I see 2 posibilities:
- Elements are not loaded
Use implicit or explicit wait : https://www.geeksforgeeks.org/waits-in-selenium-python/?ref=rp
- Xpath is not correct
Maybe learn how to use relative path : https://www.tutorialspoint.com/xpath/xpath_relative_path.htm
I have navigated to a (https://gmet.edupage.org/) webpage, logged in and got to the final step to complete my code, which is to click a next day arrow which cycles through the days of a calendar. Thing is while I have done this many times before the program still returns an error selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"next.fa.fa-angle-right"}
I wonder why cant it get the element when the source webpage code looks like this:
<span class="next fa fa-angle-right" data-smer="1" style="display:inline-block"></span>
My code looks like this:
next_page_button = driver.find_element_by_x('.next.fa.fa-angle-right')
next_page_button.click()
Any ideas on why this migh not work?
this should work:
next_page_button = driver.find_element_by_xpath("//span[#class='next fa fa-angle-right']")
next_page_button.click()
The issue was pretty simple I just had to add a time.sleep command so that the page has time to load.
I am trying to scrape a JS website. I used Selenium and everything works well until I got this error. The website has a table and I am working on it.
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/table/tbody/tr[2]/td[2]/table/tbody/tr[26]/td[4]"}
(Session info: chrome=84.0.4147.125)
In the XPath
25th and 27th iterations have 4. td
/html/body/table/tbody/tr[2]/td[2]/table/tbody/tr[26]/td[4]
but 26th doesn't have a 4th td.
I try to solve the problem with using "pass in loop" like this:
if(bool(driver.find_element_by_xpath("/html/body/table/tbody/tr[2]/td[2]/table/tbody/tr[26]/td[***+str(j)+***] ").is_enabled)):pass
But as soon as selenium sees the path it is giving an error without checking. I understand that "is_enable" method works for nothing.
The question is: how can I pass the iteration without Selenium interrupt?
try:
driver.find_element_by_xpath("./html/body/table/tbody/tr[2]/td["+str(j)+"]/table/tbody/tr["+str(i)+"]/td[4]")
except NoSuchElementException:
continue
If there is no element in the XPath, It returns null. It works. Thank you #JaSon
Have you tried something like this? (You can easily boot it up with for loop)
if driver.find_element_by_xpath("/html/body/table/tbody/tr[2]/td[2]/table/tbody/tr[26]/td[1(Can't boosted with for loop):
print("Found object")
else:
print("Can't find object")