I am trying to find the following element in selenium
It is a user name input field and I use: loginLink = driver.find_element(By.name, "loginEmail" but keep getting "no such element" message.
//input[#ng-reflect-name='loginEmail']
Use xpath or CSS , you can find by name only if the attribute key is 'name'
Eg 'name=loginEmail'
driver.find_element_by_xpath("//input[#ng-reflect-name='loginEmail']")
driver.find_element_by_css_selector("input[ng-reflect-name='loginEmail']")
you can use xpath and css for any attribute as
xpath: //tagname[#attriubute='value']
css: tagname[attriubute='value']
Using the xpath given by PDHide the code you need to use is
loginLink = driver.find_element_by_xpath("//input[#ng-reflect-name='loginEmail']")
Related
the html tag
<div class=""><div>Bengaluru, Karnataka</div></div>
Consider the above example for reference.
I tried the following code but it doesn't work!!!
driver.find_element(By.XPATH,'//div[#class=""]').text.strip()
You can not filter by that "==$0"
But you can use this xpath, which will return to you the element with following requirements:
It is a "div"
That "div" contains an attribute "class"
That "class" attribute has a length of 0 chars
This is the xpath:
//div[#class[string-length()=0]]
You can use this:
driver.find_element(By.XPATH, ".//div[#class='']/div").text
Output:
Bengaluru, Karnataka
I have an input tag html element that Selenium Python fails to identify (not because of the wait). So on a web page with a form (name is Form1), I want to extract the text in one of the fields. This is the html element here when I inspect the elements on chrome:
Input Element:
<input name="txtSerialNo" type="text" readonly="readonly" id="txtSerialNo" class="tbFormRO" style="width:160px;position:absolute;left:90px;top:7px;text-align:center;">
The full xpath is this when I right-click on the element to copy the xpath: /html/body/form/div[9]/input[1]
The HTML Element
There isn't any text on it, so I tried the below and all did not work. I also tried the implicit wait and WebDriverWait. They are irrelevant and did not work.
driver.maximize_window()
driver.find_element_by_xpath('/html/body/form/div[9]/input[1]')
driver.find_element_by_id('txtSerialNo')
driver.find_element_by_name("txtSerialNo")
driver.find_element_by_xpath("//input[#id='txtSerialNo']")
It all returns error:
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="txtSerialNo"]"}
(Session info: chrome=91.0.4472.114)
My question is: I can see that when I inspect the element, the text I want to retrieve is in the Property tab, under input#txtSerialNo.tbFormRO
Under Property
I am using a for loop to gather all input element, but I don't know how to extract that "value" property under the "category" of input#txtSerialNo.tbFormRO in the property tab when I inspect the element. Sorry I don't have a solid CSS/HTML knowledge.
The Text I Want to Extract
I tried the below without success:
for inp in driver.find_elements_by_xpath('//form[#name="Form1"]//input'):
for k in inp.get_property('attributes')[0].keys():
print(inp.get_attribute(k))
for inp in driver.find_elements_by_xpath('//form[#name="Form1"]//input'):
print(inp.value_of_css_property('value'))
# get_property(input#txtSerialNo.tbFormRO.text)
# .get_attribute('text')
# .get_attribute("innerHTML")
# .get_attribute('value')
# .get_property('input#txtSerialNo.tbFormRO.value')
I think you are looking for .get_attribute(). Based on the image lets adjust the xpath to '//input[#name="txtSerialNo"]'
for inp in driver.find_elements_by_xpath('//input[#name="txtSerialNo"]'):
print(inp.get_attribute('value'))
error occurred(missing element) in both ways :
ele3=d.find_element_by_css_selector("input[name='tel'][type='data-v-094823ec']")
ele3=browser.find_element_by_xpath("//input[#placeholder='Please enter your phone number']")
webpage element:
For the CSS selector,
input[name='tel'][type='data-v-094823ec']
should be
input[type='tel'][data-v-094823ec]
See if this works:
driver.find_element_by_xpath(".//div[#class='van-dropdown-menu']/input[#type='tel']")
I am trying to access the text of multiple tags using selenium in python.
The tags do not have attribute like id or class; they have an attribute named itemprop.
For instance there are multiple tags of such type:
<p itemprop="articleBody">
London's Gatwick Airport ........</p>
I can't use "select element by tag name" because there are tag "p" with different attributes which I don't want to include.
I am using the below code to select these elements:
elements = driver.find_element(By.CSS_SELECTOR, """p[itemprop='articleBody’]""")
However it throws the error - ......
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"p[itemprop='articleBody’]"}
How can I fix this?
you have smart quote ’ in the selector, use find_elements* with s to get multiple elements.
elements = driver.find_elements(By.CSS_SELECTOR, 'p[itemprop="articleBody"]')
# Or
elements = driver.find_elements_by_css_selector('p[itemprop="articleBody"]')
I am using Selenium for Python 2.7.10.
With XPath, I would like to locate the link in a href, following the sibling to minimal-list__title (i.e. I'm looking for the child beneath minimal-list__value). Which XPath should I use?
<span class="minimal-list__title">ETF Home Page:</span>
<span class="minimal-list__value">
ROBO
This is the current attempt:
from selenium import webdriver as driver
from selenium.common.exceptions import NoSuchElementException
def get_link(driver, key):
key = key + ":"
try:
find_value = driver.find_element_by_xpath("//span[#class='minimal-list__title' and . = '%s']/following-sibling::span/*[1]::a" % key).text
except NoSuchElementException:
return None
else:
value = re.search(r"(.+)", find_value).group().encode("utf-8")
return value
website = get_link(driver, "ETF Home Page")
print "Website: %s" % website
Note that I am specifically interested in a XPath that gets the link from the child of the following sibling. This is because the function above uses "ETF Home Page:" in the web code as an identifier for what to search for.
You're almost correct:
//span[#class = "minimal-list__title" and . = "ETF Home Page:"]/following-sibling::span/a
Note that you don't need to worry about multiple elements matching the locator since you are using find_element_by_xpath() and it would give you the first matching element.
Though, if it would makes sense in your case and you know the "ROBO" label beforehand:
driver.find_element_by_link_text("ROBO")
To get an attribute value, use get_attribute():
find_value = driver.find_element_by_xpath('//span[#class = "minimal-list__title" and . = "ETF Home Page:"]/following-sibling::span/a').get_attribute("href")
String e = driver.findElement(By.xpath("//*[contains(#class,"minimal-list__value")]/a)).getAttribute("href");
//*[contains(#class,"minimal-list__value")]/a is the xpath, the getAttribute will give you the desired result.
Based on the text ETF Home Page: to extract the link http://www.robostoxetfs.com/ from the child node of the following sibling you can use either of the following xpath based Locator Strategies:
Using xpath and following-sibling:
print(driver.find_element_by_xpath("//span[text()='ETF Home Page:']//following-sibling::span/a").get_attribute("href"))
Using xpath and following:
print(driver.find_element_by_xpath("//span[text()='ETF Home Page:']//following::span/a").get_attribute("href"))