Cant click on button by css_selector in python with selenium - python

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.

Related

I have "no such element: Unable to locate element" error although ID is 100% correct

I am trying to click on "OK" button on a pop up using selenium and Python but i face an error "no such element: Unable to locate element" although being sure that my id is 100% correct.
> <a class="dxm-content dxm-hasText dx dxalink" href="javascript:;" role="menuitem" id="Dialog_PAC_Menu_DXI0_T"><span class="dx-vam dxm-contentText">OK</span></a>
My python selenium code:
Export2 = driver.find_element(By.XPATH,'//a[#id="Dialog_PAC_Menu_DXI0_T"]')
Export2.click()
Where exactly did I go wrong, i also tried full Xpath, wait till clickable, time sleep. everything!
i would appreciate if someone can help me with it.
Well, it looks like you found the answer and that was because your element was inside an iFrame so I will just post an answer here so others can find an easy answer if they view your question.
tmpheader = driver.find_element(By.CSS_SELECTOR, "#modal > iframe")
driver.switch_to.frame(tmpheader)
Export2 = driver.find_element(By.XPATH,'//a[#id="Dialog_PAC_Menu_DXI0_T"]')
Export2.click()
This switches our current driver that we are looking for into the first iFrame element on the page. If there is more than one iFrame you would need to check for more attributes in the first line to make sure you are looking at the correct one
Actually that's how I solved my issue as my element was inside an iFrame as John Gordon suggested.
driver.switch_to.frame(driver.find_element(By.TAG_NAME,'iframe'))
slider = driver.find_element(By.XPATH,"//*[contains(#id,'_xaf_dviImportExportFormat_Edit_dropdown_DD_B-1Img')]").click()

Unable to find Button python selenium

This code:
bump = driver.find_element_by_class_name("rlg-trade__action rlg-trade__bump --bump ")
Code
Generates this error:
Unable to locate element:
{"method":"css selector","selector":".rlg-trade__action rlg-trade__bump --bump "}
Error
Here's the HTML code:
HTML Code
Did you notice on the error message that Selenium appended a . to the class name. So doing this:
driver.find_element_by_class_name("rlg-trade__action rlg-trade__bump --bump ")
Made Selenium look for an element which matched this selector:
.rlg-trade__action rlg-trade__bump --bump
When what you wanted was something like this:
.rlg-trade__action.rlg-trade__bump.--bump
If you look the documentation up, it seems that find_element_by_class_name only allows you to find an element by a single classname.
So if you really want to go around this by using that selector, maybe find_element_by_css_selector makes more sense:
driver.find_element_by_css_selector('.rlg-trade__action.rlg-trade__bump.--bump')

Python Selenium didn't find Element by ID

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

Unable to locate element with xPath

I'm literally going crazy to find an element in a specific web page. It's an "Enter" button but I haven't been being able to locate it.
ERROR MESSAGE:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"input"}
(Session info: chrome=86.0.4240.111)
I'm going to share with you the possible selectors by ChroPath Extension:
HTML INSPECT CODE "ENTER" BUTTON:
<input type="button" value="Entra" onclick="parent.location.href='site.site.site'">
1°ATTEMPT - PYTHON CODE WITH REL XPATH:
elem = browser.find_element_by_xpath('//body[1]/div[1]/div[1]/div[2]/div[1]/table[1]/tbody[1]/tr[1]/td[2]/table[1]/tbody[1]/tr[3]/td[1]/input[1]')
2°ATTEMPT - PYTHON CODE WITH ABS XPATH:
elem = browser.find_element_by_xpath('/html[1]/body[1]/div[1]/div[1]/div[2]/div[1]/table[1]/tbody[1]/tr[1]/td[2]/table[1]/tbody[1]/tr[3]/td[1]/input[1]')
I have checked if there were any iframes, but I can't find them.
Please, help me.
I agree with #josifoski. It is better to use custom xpath than autogenerated by browser. So in your case try to use the following xpath:
xpath = '//input[#type="button"][#value="Entra"]'
It will be easier to understand and support.
I got it.
I was wrong when I told you that there weren't any iframes in the source code.
There are 2 ones!
So this is the right code to click the "Enter" button:
browser.switch_to.frame(0)
browser.switch_to.frame(1)
elem = browser.find_element_by_css_selector('td:nth-child(2) input')
elem.click()
To figure out, I recorded my steps with Selenium Ide for Chrome Browser, then I exported the python source code and I saw 2 switch_to.frame functions; then I did 2 plus 2...
Like you can see, at the end, I used a css_selector argument based on the recorded source code that I had exported.
I hope it's clear and useful for other people.
Thank you so much for your help and sorry for the incomplete information.
Can you try this
xpath = '//input[#value="Entra"]'

How can I check next iteration in for loop?

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")

Categories

Resources