Unable to find an element in selenium python - python

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']")

Related

Selenium: Unable to locate element by class and id

Trying to scrape a website, I created a loop and was able to locate all the elements. My problem is, that the next button id changes on every page. So I can not use the id as a locator.
This is the next button on page 1:
<a rel="nofollow" id="f_c7" href="#" class="nextLink jasty-link"></a>
And this is the next button on page 2:
<a rel="nofollow" id="f_c9" href="#" class="nextLink jasty-link"></a>
Idea:
next_button = browser.find_elements_by_class_name("nextLink jasty-link")
next_button.click
I get this error message:
Message: no such element: Unable to locate element
The problem here might be that there are two next buttons on the page.
So I tried to create a list but the list is empty.
next_buttons = browser.find_elements_by_class_name("nextLink jasty-link")
print(next_buttons)
Any idea on how to solve my problem? Would really appreciate it.
This is the website:
https://fazarchiv.faz.net/faz-portal/faz-archiv?q=Kryptow%C3%A4hrungen&source=&max=10&sort=&offset=0&_ts=1657629187558#hitlist
There are two issues in my opinion:
Depending from where you try to access the site there is a cookie banner that will get the click, so you may have to accept it first:
browser.find_element_by_class_name('cb-enable').click()
To locate a single element, one of the both next buttons, it doeas not matter, use browser.find_element() instead of browser.find_elements().
Selecting your element by multiple class names use xpath:
next_button = browser.find_element(By.XPATH, '//a[contains(#class, "nextLink jasty-link")]')
or css selectors:
next_button = browser.find_element(By.CSS_SELECTOR, '.nextLink.jasty-link')
Note: To avoid DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() import in addition from selenium.webdriver.common.by import By
You can't get elements by multiple class names. So, you can use find_elements_by_css_selector instead.
next_buttons = browser.find_elements_by_css_selector(".nextLink.jasty-link")
print(next_buttons)
You can then loop through the list and click the buttons:
next_buttons = browser.find_elements_by_css_selector(".nextLink.jasty-link")
for button in next_buttons:
button.click()
Try below xPath
//a[contains(#class, 'step jasty-link')]/following-sibling::a

Extract Text from CSS Property using Selenium Python

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'))

Python selenium webdriver select href select partial text

I'm using Python 3.8.5 on Ubuntu 20.04. Using selenium webdriver on chrome, I want to download the attachment by specifying the licenceId number (1467250) which is included in this element:
<a xmlns="http://www.w3.org/1999/xhtml" href="#" onclick="if(typeof jsfcljs == 'function'){jsfcljs(document.forms['myApplicationsResultsForm'],'myApplicationsResultsForm:searchResultsTable:0:j_id339:0:j_id340,myApplicationsResultsForm:searchResultsTable:0:j_id339:0:j_id340,licenceId,1467250,statusHistoryId,2600790,fileName,ROL_1467250_20200817-142839.pdf,attachmentType,ROL','');}return false" class="pageLink"><img src="/oplinc2/images/pdf.jpg" alt="ROL_1467250_20200817-142839.pdf" height="24" style="border-width: 0px;" title="ROL_1467250_20200817-142839.pdf" width="24" /></a>
I am able to download this link by clicking on the css_selector:
pdf = driver.find_element_by_css_selector('#myApplicationsResultsForm\:searchResultsTable\:0\:j_id335 > a')
pdf.click()
Am I able to use partial text within the element to locate and download attachment eg. licenceID, 1467250? There are many of these attachments. I tried the partial text example from the docs but this didn't work for me:
>>> driver.find_element_by_partial_link_text('1467250')
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"partial link text","selector":"1467250"}
Edit
This question is similar to #Ajay link to this solution except this element has slightly different href. Still not sure how to access onclick
try this
links = driver.find_elements_by_partial_link_text('https://websites.com/activation.php?a=act')
for link in links:
print(link.get_attribute("href"))
driver.find_element_by_xpath('//*[contains(#onclick, "1467250")]')
Replacing the a with * finds the element.

How can I located element in selenium using the following element?

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']")

How to click on the element with Selenium Python

I'm trying to click a the element with text as I don't have the telephone on this website.
So I find the element with inspect. here is the element in html:
<span class="toggle-link link_has-no-phone" role="button">I don't have a telephone number</span>
In my nonfunctional code i wrote this:
r = driver.find_element_by_xpath("//*[#id='root']/div/div[2]/div/main/div/div/div/form/div[3]/div/div[2]/div/span")
r.click
The button is never clicked and nothing happens i get no error and i can't click it any help would be appreciated.
You can use css selector below to get span:
r = driver.find_element_by_css_selector(".link_has-no-phone")
r.click()
r = driver.find_element_by_xpath("//*[#id='root']/div/div[2]/div/main/div/div/div/form/div[3]/div/div[2]/div/span")
r.click()
You just forgot the parenthesis
To click on the element with text as I don't have a telephone number you can use either of the Locator Strategies:
css_selector:
driver.find_element_by_css_selector("span.toggle-link.link_has-no-phone").click()
xpath:
driver.find_element_by_xpath("//span[#class='toggle-link link_has-no-phone']").click()

Categories