I am using python and selenium to test some things with fantasy football. Here is my code so far (I just started).
import time
from selenium import webdriver
driver = webdriver.Chrome('C:\\Users\\202300Fontenot\\Desktop\\3\\chromedriver.exe')
driver.get('http://games.espn.com/ffl/signin?redir=http%3A%2F%2Fgames.espn.com%2Fffl%2Fclubhouse%3FseasonId%3D2018%26leagueId%3D49607%26teamId%3D4');
driver.implicitly_wait(10)
time.sleep(10)
search_box = driver.find_element_by_xpath('//*[#id="did-ui-view"]/div/section/section/form/section/div[1]/div/label/span[2]/input')
search_box.send_keys('email#icloud.com')
search_box.submit()
time.sleep(5)
driver.quit()
This just tries to enter an email address into the box. I am getting this error every time:
Traceback (most recent call last):
File "C:\Users\202300Fontenot\Desktop\3\ESPN.py", line 8, in <module>
search_box = driver.find_element_by_xpath('//*[#id="did-ui-view"]/div/section/section/form/section/div[1]/div/label/span[2]/input')
File "C:\Users\202300Fontenot\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 393, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "C:\Users\202300Fontenot\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 966, in find_element
'value': value})['value']
File "C:\Users\202300Fontenot\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 320, in execute
self.error_handler.check_response(response)
File "C:\Users\202300Fontenot\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id="did-ui-view"]/div/section/section/form/section/div[1]/div/label/span[2]/input"}
(Session info: chrome=69.0.3497.92)
(Driver info: chromedriver=2.42.591088 (7b2b2dca23cca0862f674758c9a3933e685c27d5),platform=Windows NT 10.0.16299 x86_64)
Thanks for your help.
Your problem is that the input you are trying to select exists inside of an iframe. You must first tell the driver to switch to the iframe and then execute the xpath selection.
driver.switch_to.frame("disneyid-iframe")
driver.find_element_by_xpath("//input[#type='email']").send_keys('email#icloud.com')
First of all you need to switch to your iframe using its name:
disneyid-iframe
Also if XPATH is not the only choice, you may use the CSS Selector:
div.field-username-email input[type='email']
This will get the driver to find & fill your field.
Using XPath in python You need to provide the string for
driver.find_element_by_xpath
Example Code below
("//*[#id=\"did-ui-view\"]/div/section/section/form/section/div[1]/div/label/span[2]/input")
Please refer below document regarding String
String Gude first Para
Related
I am running some testing on our website using selenium.
At the login page I would like to target the login button and click it.
the source code of the page looks like this:
I am trying to target the second button that has the class=OTSigninButton by using its xpath.
so here is the python code.
time.sleep(5)
element = driver.find_element_by_xpath('//*[#id="root"]/div/div[1]/div/svg[2]/path')
element.click()
but when I run the code I get the following error:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id="root"]/div/div[1]/div/svg[2]/path"}
(Session info: chrome=91.0.4472.106)
Here is the weird part. The login page has 2 buttons, their xpath is similar as its a list.
the first button is:
//*[#id="root"]/div/div[1]/div/svg[1]/path
and the second button(the one I want to target) is
//*[#id="root"]/div/div[1]/div/svg[1]/path
As both of them has the id=root, if I find_element_by_id and target root, I am able to click on the first button.
Here is where I am struggling a lot, how can I use the xpath to target the second button?
thank you so much for your time and help guys.
EDIT: Full error
Traceback (most recent call last):
File "test.py", line 17, in <module>
element = driver.find_element_by_xpath("//*[local-name()='svg' and contains(#class, 'OTSigninButton')]/title")
File "/Users/<user>/opt/anaconda3/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 394, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "/Users/<user>/opt/anaconda3/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 976, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "/Users/<user>/opt/anaconda3/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/Users/<user>/opt/anaconda3/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[local-name()='svg' and contains(#class, 'OTSigninButton')]/title"}
(Session info: chrome=91.0.4472.106)
class=OTSigninButton
is a attribute of SVG. These are special tags, you can not just write their tag name in console and locate them.
try this instead :
//*[local-name()='svg' and contains(#class, 'OTSigninButton')]
I am trying to edit chrome browsers search and homepage using selenium/python. After navigating to chrome://settings/searchEngines and targeting the 'add' button with the ID 'addSearchEngine', I get an error when I run a .click function. How do I target this element correctly, or is there another way to update chromes search/startpage with python?
I'm guessing this element is trapped inside an iframe but I'm unable to find one on the page using the dev tools, xpath noted the following about the absolute xpath: "It might be a child of iframe from different src & it is not supported currently."
from selenium import webdriver
driver = webdriver.Chrome()
driver.set_page_load_timeout(10)
driver.get("chrome://settings/searchEngines")
driver.find_element_by_id("addSearchEngine").click()
Traceback (most recent call last):
File "C:/Users/Jonathan/PycharmProjects/test_project/test_project/Main.py", line 20, in <module>
driver.find_element_by_id("addSearchEngine").click()
File "C:\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 360, in find_element_by_id
return self.find_element(by=By.ID, value=id_)
File "C:\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
'value': value})['value']
File "C:\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Python37-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="addSearchEngine"]"}
(Session info: chrome=75.0.3770.142)
chrome://settings/searchEngines has Shadow DOM elements . You will need to use the driver.execute_script() to get handle of the shadowRoot element and ultimately get to the 'addSearchEngine' element.
Example python : shadowRoot python
Example Java : For chrome://downloads/ shadowRoot java
Refer to this post for detailed explanation.
In your case you can do the below.
url = "chrome://settings/searchEngines"
driver.get(url)
addButton = driver.execute_script("return document.querySelector('settings-ui')"
".shadowRoot.querySelector('#main')"
".shadowRoot.querySelector('settings-basic-page.showing-subpage')"
".shadowRoot.querySelector('settings-search-page')"
".shadowRoot.querySelector('settings-search-engines-page')"
".shadowRoot.querySelector('#addSearchEngine')")
addButton.click()
Screenshot:
Why is it whenever I try to find "uxStartDateDecisionTextBox" it says it cannot find the ID. On the website they have another ID called "BodyPlaceHolder_uxTextSearchKeywords" and whenever I try to find that ID it works perfectly fine. What is the issue? I tried googling it but nothing helped, so I am resorting to asking on here.
Code:
from selenium import webdriver
url = "http://www.fareham.gov.uk/casetrackerplanning/applicationsearch.aspx"
driver = webdriver.Chrome(executable_path=r"C:\Users\Goten\Desktop\chromedriver.exe")
driver.get(url)
driver.find_element_by_id("lnkAllowCookies").click()
driver.find_element_by_id("BodyPlaceHolder_uxLinkButtonShowAdvancedSearch").click()
driver.find_element_by_id("uxStartDateDecisionTextBox").click()
driver.find_element_by_id("uxStartDateDecisionTextBox").clear()
driver.find_element_by_id("uxStartDateDecisionTextBox").send_keys("01/08/2018")
driver.find_element_by_id("uxStopDateDecisionTextBox").click()
driver.find_element_by_id("uxStopDateDecisionTextBox").clear()
driver.find_element_by_id("uxStopDateDecisionTextBox").send_keys("30/08/2018")
driver.find_element_by_id("BodyPlaceHolder_uxButtonSearch").click()
Error:
Traceback (most recent call last):
File "C:\Users\Goten\Desktop\sel.py", line 11, in <module>
driver.find_element_by_id("uxStartDateDecisionTextBox").click()
File "C:\Users\Goten\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 359, in find_element_by_id
return self.find_element(by=By.ID, value=id_)
File "C:\Users\Goten\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 966, in find_element
'value': value})['value']
File "C:\Users\Goten\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 320, in execute
self.error_handler.check_response(response)
File "C:\Users\Goten\Anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"id","selector":"uxStartDateDecisionTextBox"}
(Session info: chrome=68.0.3440.106)
(Driver info: chromedriver=2.41.578737 (49da6702b16031c40d63e5618de03a32ff6c197e),platform=Windows NT 10.0.17134 x86_64)
Before one clicks on the 'Advanced Search' link, the entire Advanced Search Form is not at all present in the DOM. After clicking on the link, the entire form is loaded in the DOM at runtime. So I can't think of anything other than this being a synchronization issue.
Try adding a step before
"driver.find_element_by_id("uxStartDateDecisionTextBox").click()" that pauses the execution for a second or so. If after that, Selenium is able to locate the element, you can be pretty sure that this is a synchronization problem. Then you can opt for some kind of explicit waits to improve your execution speed.
Hi I'm very new to Selenium and would really appreciate help.
I need to learn how to copy text, particularly from a table from a specific website: https://aca.tampagov.net/citizenaccess/Default.aspx#
(Go to url --> Search --> Building Permits --> click search button).
You can finds a table at the bottom of the page.
The first row in column "Record Type" has text "Commercial Utility Application". If I want to copy the text what I thought I should do is get the xpath which is
//*[#id="ctl00_PlaceHolderMain_dgvPermitList_gdvPermitList_ctl02_lblType"]
So I would have something like
driver.find_element_by_xpath('//*[#id="ctl00_PlaceHolderMain_dgvPermitList_gdvPermitList"]/tbody/tr[3]/td[4]').text()
But this gives me and error. What am I doing wrong here? And how would I be able to copy texts from such tables?
EDIT
So here's what I get:
Traceback (most recent call last):
File "<ipython-input-81-4e51d9229198>", line 1, in <module>
driver.find_element_by_xpath('//* [#id="ctl00_PlaceHolderMain_dgvPermitList_gdvPermitList"]/tbody/tr[3]/td[4]').text()
File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 387, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 957, in find_element
'value': value})['value']
File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 314, in execute
self.error_handler.check_response(response)
File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id="ctl00_PlaceHolderMain_dgvPermitList_gdvPermitList"]/tbody/tr[3]/td[4]"}
(Session info: chrome=67.0.3396.99) (Driver info: chromedriver=2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb),platform=Windows NT 10.0.17134 x86_64)
Your table is in iframe, so you have to switch to it before you can interact with this table. This code snippet will help you to do it:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//*[#id='ACAFrame']")))
# do your stuff
text = driver.find_element_by_xpath('//*[#id="ctl00_PlaceHolderMain_dgvPermitList_gdvPermitList"]/tbody/tr[3]/td[4]').text()
print(text)
driver.switch_to.default_content() # switch back to default content
PS: since in the page there is only one table, you can simplify your xPath from:
//*[#id="ctl00_PlaceHolderMain_dgvPermitList_gdvPermitList"]/tbody/tr[3]/td[4]
to:
//tbody/tr[3]/td[4]
To retrieve text Commercial Utility Application, you should try -
driver.findElement(By.id("ctl00_PlaceHolderMain_dgvPermitList_gdvPermitList_ctl02_lblType")).getText();
I have an issue locating element in Python Selenium using href within XPath. I have checked that my XPath is correct using Chrome console and I am happy with the result it returns.
I used the following to validate XPath on Chrome Console:
$x("//li[#id='result_0']//a[#href='https://www.amazon.co.uk/HuntGold-Plastic-Outdoor-Carrier-Container/dp/B00R7DCPWO/ref=sr_1_1?ie=UTF8&qid=1476540708&sr=8-1&keywords=banana+holder#customerReviews']")
Then I used the same XPath in my Python Selenium code:
path = "//li[#id='result_0]//a[#href='https://www.amazon.co.uk/HuntGold-Plastic-Outdoor-Carrier-Container/dp/B00R7DCPWO/ref=sr_1_1?ie=UTF8&qid=1476540708&sr=8-1&keywords=banana+holder#customerReviews']"
reviews = driver.find_element_by_xpath(path).text
and I got the following error message:
reviews = driver.find_element_by_xpath(reviews_path).text
File "/Users/Paulius/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 293, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "/Users/Paulius/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 752, in find_element
'value': value})['value']
File "/Users/Paulius/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File "/Users/Paulius/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 192, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element
(Session info: chrome=53.0.2785.143)
(Driver info: chromedriver=2.16.333243 (0bfa1d3575fc1044244f21ddb82bf870944ef961),platform=Mac OS X 10.12.0 x86_64)
Does anyone know how to get href XPath in Python Selenium working?