Using Python 3+ and Selenium > IE Driver
I am running a python script to automate filling out a form on a page. The reason for finding the parent element is because there are multiple elements with the same class and no other identifiers other then the text based title.
HTML:
<div class="pg_BoxContents"> /* Div I am trying to select */
<div class="Title">Replay Permissions:</div>
</div>
Python / Selenium:
replay_form = browser.find_element_by_xpath("//span[contains(text(),'Replay Permissions:')]/ancestor::div[contains(#class, 'pg_BoxContents')]")
Error:
selenium.common.exceptions.NoSuchElementException: Message: Unable to find element with xpath == //span[contains(text(),'Details:')]/ancestor::div[contains(#class, 'pg_BoxContents')]
Replace:
//span[contains(text(),'Replay Permissions:')]/ancestor::div[contains(#class, 'pg_BoxContents')]
with:
//div[contains(text(),'Replay Permissions:')]/ancestor::div[contains(#class, 'pg_BoxContents')]
You are using //span[... where there is no span in HTML you have provided. But there is a div.
Related
I am trying to pull the price of the first listing on this website with the following code, but it's returning nothing but blanks. I am navigating to the website, hitting F12, and then copying the XPATH into the line of code below. Any thoughts on why this wouldn't work?
the following gives nothing:
/html/body/div[1]/div/section[6]/div/div[3]/div/div[1]/a/div/div[1]/div/div[2]/div/div[2]/span/sup
While this shows the listing seller successfully:
/html/body/div[1]/div/section[6]/div/div[3]/div/div[1]/a/div/div[1]/div/div[2]/div/div
driver.get('https://swappa.com/mobile/buy/apple-iphone-xs/t-mobile')
pricing = WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.XPATH, '//*[#id="listing_previews"]/div[1]/a/div/div[1]/div/div[2]/div/div[2]/span/sup'))).text
Use getattribute and switch to the span.
pricing = WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.XPATH, '//*[#id="listing_previews"]/div[1]/a/div/div[1]/div/div[2]/div/div[2]/span')))
print(pricing.get_attribute('textContent'))
Outputs
$379
The reason you are not getting values using element.text because span element is hidden by parent tag.
<div class="col-xs-3 col-sm-2 hidden-md hidden-lg text-right">
<span class="price"><sup>$</sup>379</span>
</div>
Instead of .text you need to use textContent attribute which retrieves the value from hidden nodes.
Use the following code block to retrieve all the products price.
driver.get('https://swappa.com/mobile/buy/apple-iphone-xs/t-mobile')
allproductdetails=WebDriverWait(driver,10).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR,"div.listing_row")))
for prod in allproductdetails:
print(prod.find_element_by_css_selector("div.media-body span.price").get_attribute("textContent"))
I am using selenium and python to learn about automation web testing.
I want to click on "Continue" button, while there is only span in it (I had learned that using id instead of span is so much easier)
but in this case, I want to click the span.
I am using below code:
driver.find_element_by_xpath("//span[contains(#class,'mdBtn01Txt')][contains(text(),'Continue')]").click()
here is the element :
<div class="mdLYR12Inner01">
<a class="MdBtn01 mdBtn01 ExDisabled FnSubmitBtn" style="display:none" href="#" onclick="charge(this); return false;">
<span class="mdBtn01Inner">
<span class="mdBtn01Txt">
Continue <<<(I want to click this button)
</span>
</span>
</a>
</div>
</footer>
But, I got this message below:
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//span[contains(#class,'mdBtn01Txt')][contains(text(),'Continue')]"}
Does not looks like a valid xpath, correct one should be
//span[#class='mdBtn01Txt']
Here is the rule to create xpath
Syntax for XPath:
XPath contains the path of the element situated at the web page. Standard syntax for creating XPath is:
Xpath=//tagname[#attribute='value']
Tagname: Tagname of the particular node.
#: Select attribute.
Attribute: Attribute name of the node.
Value: Value of the attribute.
This is my HTML code:
<button class="_qv64e _gexxb _r9b8f _njrw0">Follow</button>
But, when I try to click on this this error is occurred:
selenium.common.exceptions.NoSuchElementException: Message: Unable to
locate element: span._qv64e _gexxb _r9b8f _njrw0
It is happened in any way that I used, for example in xpath, CSS selector, tag name and ... . This error has been occurred through this code:
driver.find_element_by_css_selector("span._qv64e _gexxb _r9b8f _njrw0").click()
When using css_selector spaces has meaning. The selector you are using tell the driver to look for an element with <_njrw0> tag which has an ancestor with <_r9b8f> tag as so on. The button also has <button> tag, not <span> tag.
You need to use . in front of any class name and without spaces
driver.find_element_by_css_selector("button._qv64e._gexxb._r9b8f._njrw0").click()
What is wrong in the below code
import os
import time
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("http://x.x.x.x/html/load.jsp")
elm1 = driver.find_element_by_link_text("load")
time.sleep(10)
elm1.click()
time.sleep(30)
driver.close()
The page source is
<body>
<div class="formcenterdiv">
<form class="form" action="../load" method="post">
<header class="formheader">Loader</header>
<div align="center"><button class="formbutton">load</button></div>
</form>
</div>
</body></html>
I want to click on button load. when I ran the above code getting this error
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: load
As the documentation says, find_elements_by_link_text only works on a tags:
Use this when you know link text used within an anchor tag. With this
strategy, the first element with the link text value matching the
location will be returned. If no element has a matching link text
attribute, a NoSuchElementException will be raised.
The solution is to use a different selector like find_element_by_class_name:
elm1 = driver.find_element_by_class_name('formbutton')
Did you try using Xpath?
As the OP said, find_elements_by_link_text works on a tags only:
Below code might help you out
driver.get_element_by_xpath("/html/body/div/form/div/button")
I am using Python and Selenium to scrape a webpage, in some cases, I can't get it to work,
*
I would like to access the element with text 'PInt', wich is the second link in the below code.
The xPath for it (copied from Developer console) is: //[#id="submenu1"]/a[2]
<div id="divTest" onscroll="SetDivPosition();" style="height: 861px;">
<div class="menuTitle" id="title1">
</div>
<div class="subtitle" id="submenu1">
<img src="images/spacer.gif" border="0" width="2px" height="12px">
Mov<br>
<img src="images/spacer.gif" border="0" width="2px" height="12px">
PInt<br>
<img src="images/spacer.gif" border="0" width="2px" height="12px">
SWAM / SWIF<br>
</div>
...
A snipped of my code is:
try:
res = driver.find_elements_by_link_text('PInt')
print("res1:{}".format(res))
res = driver.find_element(By.XPATH,'//*[#id="submenu1"]/a[3]')
print("res:{} type[0]:{}".format(res,res[0]))
itm1 = res[0]
itm1.click()
I get the error:
Unable to locate element:
{"method":"xpath","selector":"//*[#id="submenu1"]/a[2]"}
My question is, how can I get the right xPath of the element or any other way to access the element?
UPDATE:
This might be important, the issue with
Message: invalid selector: Unable to locate an element with the xpath expression (and I've tried all proposed solutions) might be that this is after authenticate in the webpage (User + Pwd) before, everything works.
I noticed that the url driver.current_url after login is static (asp page).
Also this part I am trying to access in a frameset and frame
html > frameset > frameset > frame:nth-child(1)
Thanks to #JeffC to point me in the right direction.
since the page has some frames, I manage to access the element first by switching to the right frame (using xPath)
and then access the element.
driver.switch_to.default_content()
driver.switch_to.frame(driver.find_element_by_xpath('html / frameset / frameset / frame[1]'))
driver.find_element_by_xpath("//a[contains(text(),'PInt')]").click()
BTW, in case you want to run the script from a crontab,you need to setup a display:
30 5 * * * export DISPLAY=:0; python /usr/.../main.py
To see a full list of all the ways of selecting elements using selenium, you can read all about it in the documentation.
Using xpath:
res = driver.find_element_by_xpath(u'//*[#id="submenu1"]/a[2]')
Using css selector:
res = driver.find_element_by_css_selector('#submenu1 a:nth-of-type(2)')
Try with any of the below xpath. Sometimes automatically generated xpath does not work.
//a[contains(text(),'PInt')]
or
//div[#id='submenu1']//a[contains(text(),'PInt')]
Also I would suggest you to set some wait time before clicking on above link in case if above xpath does not work
To find xPath in chrome:
Right click the element you want
Inspect, which will open the developer window and highlight the selected element.
Right click the highlighted element and choose copy > copy by xpath
Here is a list of all the different ways to locate an element Locating Elements