I am trying to learn how to read text inside a PDF using the IE driver of Selenium. I am getting a selenium.common.exceptions.NoSuchElementException: Message: Unable to find element with css selector == body
from selenium import webdriver
import time
TO_url = "Y:\Work\Work\PFCToolbox\exampleTO\HT072663_001.pdf"
vpc_url = "http://dspgot03.vcc.ford.com/apps/vpc/vpc.nsf/"
driver = webdriver.Ie()
driver.get(TO_url)
element = driver.find_element_by_css_selector("body")
time.sleep(10)
I also tried using other driver.find_element_by functions but couldn't find one that works
Instead of finding the body element, try sending keys to get the text:
driver.send_keys(Keys.CONTROL, 'a')
driver.send_keys(Keys.CONTROL, 'c')
Then paste it from the clipboard.
Related
I'm running the following code and getting the following error:
select_survey = browser.find_element_by_class_name("ng-star-inserted").click()
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: .ng-star-inserted
this is the entire code:
import selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
browser = webdriver.Firefox()
browser.get("https://www.legeropinion.com/app/todo")
email_element = browser.find_element_by_id("email")
email_element.clear()
email_element.send_keys("something#gmail.com")
password_element = browser.find_element_by_id("password")
password_element.send_keys("password")
sign_in = browser.find_element_by_class_name("submit-btn").click()
select_survey = browser.find_element_by_class_name("ng-star-inserted").click()
I am trying to automate signing into a website and completing a form but upon clicking on the form for which the element is class = ng-star-inserted i am getting that error
edit:
this is the relevant html info i believe:
<task-block _ngcontent-buv-c2="" _nghost-buv-c9="" class="ng-star-inserted"><!----><div _ngcontent-buv-c9="" class="task ng-star-inserted"><!----><a _ngcontent-buv-c9=""
Okay so after some a lot of trial and error I finally got it to work by fluke. I don't know how but it worked!!
I simply located the element by the ABSOLUTE xpath which I copied from the HTML and then before that line i had the time sleep function like so:
time.sleep(2)
select_survey = browser.find_element_by_xpath("/html/body/app-root/ion-app/ion- router-outlet/app-todo/ion-content/mat-expansion-panel[1]/div/div/div[2]/task-block[2] /div").click()
I have started selenium using python. I am able to change the message text using find_element_by_id. I want to do the same with find_element_by_xpath which is not successful as the xpath has two instances. want to try this out to learn about xpath.
I want to do web scraping of a page using python in which I need clarity on using Xpath mainly needed for going to next page.
#This code works:
import time
import requests
import requests
from selenium import webdriver
driver = webdriver.Chrome()
url = "http://www.seleniumeasy.com/test/basic-first-form-demo.html"
driver.get(url)
eleUserMessage = driver.find_element_by_id("user-message")
eleUserMessage.clear()
eleUserMessage.send_keys("Testing Python")
time.sleep(2)
driver.close()
#This works fine. I wish to do the same with xpath.
#I inspect the the Input box in chrome, copy the xpath '//*[#id="user-message"]' which seems to refer to the other box as well.
# I wish to use xpath method to write text in this box as follows which does not work.
driver = webdriver.Chrome()
url = "http://www.seleniumeasy.com/test/basic-first-form-demo.html"
driver.get(url)
eleUserMessage = driver.find_elements_by_xpath('//*[#id="user-message"]')
eleUserMessage.clear()
eleUserMessage.send_keys("Test Python")
time.sleep(2)
driver.close()
To elaborate on my comment you would use a list like this:
eleUserMessage_list = driver.find_elements_by_xpath('//*[#id="user-message"]')
my_desired_element = eleUserMessage_list[0] # or maybe [1]
my_desired_element.clear()
my_desired_element.send_keys("Test Python")
time.sleep(2)
The only real difference between find_elements_by_xpath and find_element_by_xpath is the first option returns a list that needs to be indexed. Once it's indexed, it works the same as if you had run the second option!
I'm trying to extract data from the link below using selenium via python:
www.oanda.com
But I'm getting an error that, "Unable to Locate an Element". In browser console i tried using this Css selector:
document.querySelector('div.position.short-position.style-scope.position-ratios-app')
This querySelector returns me the data for short percentage of 1st row in the browser console(for this test), but when i used this selector in the python script below it gives me an error that, "Unable to Locate element" or sometimes empty sctring.
Please suggest me solution if there's any.Will be grateful, thanks :)
# All Imports
import time
from selenium import webdriver
#will return driver
def getDriver():
driver = webdriver.Chrome()
time.sleep(3)
return driver
def getshortPercentages(driver):
shortPercentages = []
shortList = driver.find_elements_by_css_selector('div.position.short-position.style-scope.position-ratios-app')
for elem in shortList:
shortPercentages.append(elem.text)
return shortPercentages
def getData(url):
driver = getDriver()
driver.get(url)
time.sleep(5)
# pagesource = driver.page_source
# print("Page Source: ", pagesource)
shortList = getshortPercentages(driver)
print("Returned source from selector: ", shortList)
if __name__ == '__main__':
url = "https://www.oanda.com/forex-trading/analysis/open-position-ratios"
getData(url)
Required data is located inside an iframe, so you need to switch to iframe before handling elements:
driver.switch_to.frame(driver.find_element_by_class_name('position-ratios-iframe'))
Also note that data inside iframe is dynamic, so make sure that you're using Implicit/Explicit wait (using time.sleep(5) IMHO is not the best solution)
textbox = driver.find_element_by_id('applicant.name')
doesn't work.
Unfortunately and I've tried every variation of inspect element, copy xpath , trying to identify the class name of the text boxes etc.
I want to be able to send some text to the input fields in the indeed-apply-widget seen here.
If you click 'apply now' (unfortunately I can't link it) you'll see the inputs are contained within 2 iframes.
Is there an easy way to access them?
Here's what I have so far:
from selenium import webdriver
driver = webdriver.Firefox()
driver.get('http://ca.indeed.com/cmp/Clearview-Plumbing-&-Heating/jobs/HVAC-Installer-Opening-6183dce44a9bfe10?sjdu=vQIlM60yK_PwYat7ToXhk42rccfWGklJDtD_zDpWBzDCfUEiSP2Zk-zLpFc6GNuF8wyV4_UaMyNFtpjETvX0fCpQXc3PxTbrGkwAMNkR5vGMtAfe9wNpTncItNgAIJHx' )
driver.find_element_by_class_name("indeed-apply-widget").click()
# move into the iframe
iframe = driver.find_elements_by_tag_name('iframe')[0]
driver.switch_to_frame(iframe)
Updated Code (still unable to interact with the iframe)
from selenium import webdriver
driver = webdriver.Firefox()
driver.get('http://ca.indeed.com/cmp/Clearview-Plumbing-&-Heating/jobs/HVAC-Installer-Opening-6183dce44a9bfe10?sjdu=vQIlM60yK_PwYat7ToXhk42rccfWGklJDtD_zDpWBzDCfUEiSP2Zk-zLpFc6GNuF8wyV4_UaMyNFtpjETvX0fCpQXc3PxTbrGkwAMNkR5vGMtAfe9wNpTncItNgAIJHx' )
driver.find_element_by_class_name("indeed-apply-widget").click()
# move into the iframe
path = driver.find_element_by_xpath("/html/body/div[5]/div/div[2]/iframe")
driver.switch_to_frame(path)
driver.find_element_by_xpath('//*[#id="applicant.name"]').sendKeys("TEST_NAME")
The element that you want is inside two nested IFRAMEs. The below code should work. You need to go into the first frame... and then the first nested frame. The rest should work at that point.
driver = webdriver.Firefox()
driver.get('http://ca.indeed.com/cmp/Clearview-Plumbing-&-Heating/jobs/HVAC-Installer-Opening-6183dce44a9bfe10?sjdu=vQIlM60yK_PwYat7ToXhk42rccfWGklJDtD_zDpWBzDCfUEiSP2Zk-zLpFc6GNuF8wyV4_UaMyNFtpjETvX0fCpQXc3PxTbrGkwAMNkR5vGMtAfe9wNpTncItNgAIJHx')
driver.find_element_by_css_selector("span.indeed-apply-button-label")).click()
driver.switch_to_frame(0)
driver.switch_to_frame(0)
driver.find_element_by_id("applicant.name")).send_keys(username)
driver.find_element_by_id("applicant.email")).send_keys(email)
driver.find_element_by_id("resume")).send_keys(resumePath)
driver.find_element_by_css_selector("a.button_content.form-page-next")).click()
driver.find_element_by_id("apply")).click()
I have a HTML code like this:
<div class="links nopreview"><span><a class="csiAction"
href="/WebAccess/home.html#URL=centric://REFLECTION/INSTANCE/_CS_Data/null">Home</a></span> • <span><span><a class="csiAction"
href="/WebAccess/home.html#URL=centric://SITEADMIN/_CS_Site">Setup</a></span> • </span><span><a
title="Sign Out" class="csiAction csiActionLink">Sign Out</a></span></div>
I would like to click on the link that has the text Home. As this Home link appears after login, I have a code like this:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import re
browser = webdriver.Firefox() # Get local session of firefox
browser.get("http://myServer/WebAccess/login.html") # Load App page
elem = browser.find_element_by_name("LoginID") # Find the Login box
elem.send_keys("Administrator")
elem = browser.find_element_by_name("Password") # Find the Password box
elem.send_keys("Administrator" + Keys.RETURN)
#try:
elem = browser.find_element_by_link_text("Home")
elem.click()
The part till login works great. However the last but one line is problematic
elem = browser.find_element_by_link_text("Home")
It raises this NoSuchElementException where the Home link is there as you can see from the HTML code.
raise exception_class(message, screen, stacktrace)
NoSuchElementException: Message: u'Unable to locate element: {"method":"link text","selector":"Home"}'
Any guidance as to what I am doing wrong, please?
Have you tried adding an implicit wait to this so that it waits instead of running to quickly.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import re
browser = webdriver.Firefox() # Get local session of firefox
browser.implicitly_wait(10) #wait 10 seconds when doing a find_element before carrying on
browser.get("http://myServer/WebAccess/login.html") # Load App page
elem = browser.find_element_by_name("LoginID") # Find the Login box
elem.send_keys("Administrator")
elem = browser.find_element_by_name("Password") # Find the Password box
elem.send_keys("Administrator" + Keys.RETURN)
#try:
elem = browser.find_element_by_link_text("Home")
elem.click()
The implicitly_wait call makes the browser poll until the item is on the page and visible to be interacted with.
The most common issues with NoSuchElementException while the element is there are:
the element is in different window/frame, so you've to switch to it first,
your page is not loaded or your method of page load is not reliable.
Solution could include:
check if you're using the right frame/window by: driver.window_handles,
write a wait wrapper to wait for an element to appear,
try XPath instead, like: driver.find_element_by_xpath(u'//a[text()="Foo"]').click(),
use pdb to diagnose your problem more efficiently.
See also: How to find_element_by_link_text while having: NoSuchElement Exception?
Maybe the element you are looking for doesn't exactly match that text string?
I know it can be tricky if it looks like it does on-screen, but sometimes there are oddities embedded like this simple markup "Home" or "Home" which makes the first char italic:
"<i>H</i>ome" is visually identical to "<em>H</em>ome" but does not match text.
Edit: after writing the above answer, I studied the question closer and discovered the HTML sample does show "Home" in plain text, but was not visible due to long lines not wrapping. So I edited the OP to wrap the line for readability.
New observation: I noticed that the Logout element has a "title" attribute, but the Home link element lacks such--try giving it one and using that.
Try adding an implicit wait to this in order to wait, instead of running too quickly.
Or
else you can import time and use time.sleep(25)