Python with selenium: unable to locate element which really exists - python

while len(driver.find_elements(By.XPATH,"//*[#title='Next page']"))!=0:
The element is not found (and the while loop skept), I tried adding a timer
ui.WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.XPATH,"//*[#title='Next Page']")))
Still impossible to make selenium recognize the element. I don't think I saw anything that looked like an iframe on the source (I might have missed it).

Instead of presence_of_element_located() you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following locator strategy:
ui.WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,"//*[#title='Next Page']"))).click()

Related

Find correct xpath with selenium Python

I'm currently working on a project with Selenium and webdriver. So far so good I can navigate through the website using selenium without any problem however, I would need to click on a specific icon to slides through to a date and time table. Issue is when looking at the HTML I have no idea what to locate with Selenium in order to click on that icon.
HTML sample:
<a _ngcontent-oyt-c155="" class="btn btn-soft-primary day-selector-navbutton"><i _ngcontent-oyt-c155="" class="fa fa-chevron-right"></i></a>
Usually I'm locating items by name, ID or Class but in that case I don't know where to got with.
Should I look for the xpath instead ?
Accordingly to what you have shared here this XPath should work:
'//a[#class="btn btn-soft-primary day-selector-navbutton"]//i[#class="fa fa-chevron-right"]'
I can't be completely sure if this locator is unique or if there is no iframe there etc.
You will possibly need to add some kind of wait here too. If so you should preferably use Expected Conditions explicit waits.
The desired element is a Angular element, so to click() on the clickable element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.btn.btn-soft-primary.day-selector-navbutton > i.fa.fa-chevron-right"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='btn btn-soft-primary day-selector-navbutton']/i[#class='fa fa-chevron-right']"))).click()
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Selenium(PYTHON) ng-click issue

I have been stuck on this issue, where no matter how i write out the navigation to the specific element, using .click() wont work, the element 100% loads in before the click is fired(this i know because i can manually click it), but it wont actually click, is there something about ng-click that needs specification or what, but hopefully i can atleast get some closure as to why this element wont be clicked
<div class="item-card-thumb-container" ng-click="root.onItemCardClick(userAsset)">
is there any possible way to click this element without specifically finding that element and adding .click() to it, because it doesn't seem to work, I understand its too vague, but also maybe the element isn't clickable unless the mouse hovers over the element, cause it is a UI
The desired element is a Angular element element, so ideally to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.item-card-thumb-container[ng-click^='onItemCardClick']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#class='item-card-thumb-container' and contains(#ng-click, 'onItemCardClick')]"))).click()
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

How can I switch to the iframe using Selenium and python?

I've used Selenium for a number projects without problems. This one, however, no dice.
I'm trying to automate a check out process, and sometimes there is a prompt to re-input information. The window has a javascript popup (I think), and, while I can select the <div id="app"> portion without any problems, I CANNOT get find the iframe and focus on it.
So, this works:
driver.find_elements_by_id('app')
But, nothing works to recognize the frame and switch to it. For example, none these work:
driver.find_element_by_id()
driver.find_element_by_name()
driver.find_element_by_xpath()
And, I assume that because I can't find and switch to the iframe.
Help?
The element with the text as E-Mail Login is within an <iframe> so you have to:
Induce WebDriverWait for the desired frame to be available and switch to it.
You can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe.zoid-visible[title='ec_payment'][name^='__zoid__ec__payment']")))
Using XPATH:
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#class='zoid-visible' and #title='ec_payment'][starts-with(#name, '__zoid__ec__payment')]")))
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Reference
You can find a couple of relevant discussions in:
Ways to deal with #document under iframe
Switch to an iframe through Selenium and python
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element while trying to click Next button with selenium
selenium in python : NoSuchElementException: Message: no such element: Unable to locate element
driver.switch_to.frame(iframe_elem)
or
driver.switch_to_frame(iframe_id)
in your case you can use :
driver.switch_to.frame(driver.find_element_by_tag_name("iframe"));
to switch back to main use:
driver.switch_to_default_content()
Try this code to switch to frame
frame = driver.find_element_by_xpath('//div[starts-with(#id, "zoid-ec-payment")]/iframe')
driver.switch_to.frame(frame)

Selenium on Python - Submit Method Doesn't Seem to Send Request Correctly?

I'm using Selenium in a Python script to query NASA's Small Body Database for the location of asteroids. I've been working with a Python shell to check that my commands are getting the results I want, but I'm having a consistent issue with the "submit" method.
The page I'm working on is https://ssd.jpl.nasa.gov/sbwobs.cgi. All I want to do is have Selenium click on the "change" link next to "Observation Time," fill in a field on the page that opens, and then click the resulting "Use Specified Time" button.
I've set up a Selenium WebDriver object named "browser", and then the first command I send is:
browser.find_element_by_css_selector("a[href*='time']").click()
Which works and sends me to my target page, which is https://ssd.jpl.nasa.gov/sbwobs.cgi?s_time=1#top
I then find the text field and fill it in with:
browser.find_element_by_name('obs_time').send_keys("2021-04-01 01:00")
... last thing I need is to click the "Use Specified Time" button to accept this. I tried with:
browser.find_element_by_name("check_time").click()
.. but this had no response. Then I tried:
browser.find_element_by_name("check_time").submit()
... which seems to work, but takes me back to the previous page without actually changing the time. If I manually click on the button it would work fine.
Selenium is definitely finding the right element, and the submit method is doing something, but it's maybe not sending the info in the text box correctly.
I don't know anywhere near enough about HTML etc to be able to figure this out. Can anyone offer any advice?
You were close enough. Before you set the new Observation Time as 2021-04-01 01:00 you need to clear up the previous input. Ideally, to change the Observation Time you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
driver.get('https://ssd.jpl.nasa.gov/sbwobs.cgi')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[href*='time']"))).click()
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='obs_time']")))
element.click()
element.clear()
element.send_keys("2021-04-01 01:00")
driver.find_element_by_css_selector("input[name='check_time']").click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(#href, 'time')]"))).click()
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#name='obs_time']")))
element.click()
element.clear()
element.send_keys("2021-04-01 02:00")
driver.find_element_by_xpath("//input[#name='check_time']").click()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Browser Snapshot:
Thanks to #DebanjanB for the comprehensive code that improved all my methods; however, I found that everything worked as expected with his code and mine by switching to Firefox.
Evidently it's some anomaly in Safari's web driver that was causing the issue.

How to Find Element Text within <span> using Selenium?

I am pretty new to Python and Selenium, and I have got my script to do what I want so far, but I have this current coding from the website:
<a onclick="realPostBack('ctl00$ctl00$mainContent$ContentPlaceHolder1$ucHub$ucSearchExplorer$dgContents$ctl00$ctl04$lnkContent', ''); return false;" id="ctl00_ctl00_mainContent_ContentPlaceHolder1_ucHub_ucSearchExplorer_dgContents_ctl00_ctl04_lnkContent" class="hub-content-item" actiontype="Secondary" href="javascript:__doPostBack('ctl00$ctl00$mainContent$ContentPlaceHolder1$ucHub$ucSearchExplorer$dgContents$ctl00$ctl04$lnkContent','')"><span>How to Project Wirelessly in Philly</span></a>
and I can't get it to click the link.
I've tried:
driver.find_element_by_text("How to Project Wirelessly in Philly")
and partial text
driver.find_element_by_id("ctl00_ctl00_mainContent_ContentPlaceHolder1_ucHub_ucSearchExplorer_dgContents_ctl00_ctl04_lnkContent")
I've tried by tag but all returning errors. Looking up on here, I've seen stuff with Xpath but I have no clue how to do that, but if someone here does, then a little help with that, or any other simple code that hopefully allows me to click that link. (I know i will have to do .click() to eventually click it, but i cant even find the element yet)
The desired element is a dynamic element, so invoke click() on it you have to induce WebDriverWait for the element to be clickable and you can use either of the following solutions:
Using CSS_SELECTOR:
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.hub-content-item[id*='SearchExplorer'][actiontype='Secondary']>span")))
Using XPATH:
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='hub-content-item' and contains(#id,'SearchExplorer')][#actiontype='Secondary']/span[text()='How to Project Wirelessly in Philly']")))
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Categories

Resources