Python XPath selector not working with selenium - python

I tried to automate some inputs. For this I need to input some text after the tag.
To identify the place where to input I tried XPath for fowlloing HTML code.
<span data-offset-key="1dq3m-0-0">
<br data-text="true">
</span>
Here is what I wrote in python.
buf_comp_text = 'foobar'
el_xp_comp_text = '//*[#data-text]'
...
## create post in queue (comment)
print('create post in queue - text')
post_txt = driver.find_element_by_xpath(el_xp_comp_text).send_keys(buf_comp_text)
Unfortunately I alwas get error message:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#data-text]"}
Any hint is appreciated.

Instead of targetting the <br> tag you need to target the <span> tag you can use the following Locator Strategy:
Using css_selector:
buf_comp_text = 'foobar'
driver.find_element_by_css_selector("span[data-offset-key]").send_keys(buf_comp_text)
Using xpath:
buf_comp_text = 'foobar'
driver.find_element_by_xpath("//span[#data-offset-key][.//br[#data-text]]").send_keys(buf_comp_text)
Ideally, to send a character sequence to the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategy:
Using CSS_SELECTOR:
buf_comp_text = 'foobar'
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span[data-offset-key]"))).send_keys(buf_comp_text)
Using XPATH:
buf_comp_text = 'foobar'
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[#data-offset-key][.//br[#data-text]]"))).send_keys(buf_comp_text)
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Reference
You can find a couple of relevant discussions in:
Ways to deal with #document under iframe
Switch to an iframe through Selenium and python
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element while trying to click Next button with selenium
selenium in python : NoSuchElementException: Message: no such element: Unable to locate element

Related

Selenium Python | Type in input

I am trying to type into the input category using selenium on Python google chrome. I would like to send the keys 1234 to test the configuration, however, I am receiving errors. I believe I am missing something from the code... Sorry if this is super basic, I am still learning selenium... I tried using the #class already and received an error.
driver.find_element_by_xpath("//input[='']/parent::div").send_keys('1234')
I am getting this error:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[9]/div/div/div/div[1]/div/div/div/div/div[2]/div[1]/div[1]/div[1]/div/input"}
(Session info: chrome=88.0.4324.150)
I have concluded the html should represent value=1234, however, I still am struggling with the code.
To send a character sequence to the element you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
driver.find_element(By.CSS_SELECTOR, "input.[aria-label='Amount'][placeholder='0']").send_keys("1234")
Using XPATH:
driver.find_element(By.XPATH, "//input[#aria-label='Amount' and #placeholder='0']").send_keys("1234")
The desired element is a dynamic element, so ideally to send a character sequence to the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.[aria-label='Amount'][placeholder='0']"))).send_keys("1234")
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#aria-label='Amount' and #placeholder='0']"))).send_keys("1234")
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
References
You can find a couple of relevant discussions on NoSuchElementException in:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element while trying to click Next button with selenium
selenium in python : NoSuchElementException: Message: no such element: Unable to locate element

Error on Python Selenium (find_element_by_xpath)

I am trying to write a list of code to automate my daily file downloading process. I am using Python Selenium for this. However, I am having trouble to locate one of the clicks.
HTMI Code as below:
<div class="card dark-blue"> <h3 data-mh="title-research" style="height: 52px;">Rated Range Prices (LT)</h3> <h4 class="disp_date">26-Jan-2021 17:47:58</h4> <div class="divider"></div> See More » </div>
Here is my code:
link = driver.find_element_by_xpath("//a[#href='javascript:seemore('F10002');']")
The error msg:
InvalidSelectorException: invalid selector: Unable to locate an element with the xpath expression //*[#href='javascript:seemore('F10002');'] because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//*[#href='javascript:seemore('F10002');']' is not a valid XPath expression.
(Session info: chrome=88.0.4324.104)
Can anyone please enlighten me on this? Many thanks.
To locate the element See More » you can use either of the following Locator Strategies:
Using css_selector:
element = driver.find_element(By.CSS_SELECTOR, "div.card.dark-blue a.link[href*='seemore']")
Using xpath:
element = driver.find_element(By.XPATH, "//div[#class='card dark-blue']//a[#class='link' and starts-with(., 'See More')]")
Ideally, to locate the clickable element you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.card.dark-blue a.link[href*='seemore']")))
Using XPATH:
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#class='card dark-blue']//a[#class='link' and starts-with(., 'See More')]")))
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
You're using single quotes pair inside another single quotes pair...
Try
link = driver.find_element_by_xpath('''//a[#href="javascript:seemore('F10002');"]''')

Selenium returning NoSuchElementException but the element exists

I am having this code
driver.implicitly_wait(300) # I have tried different timers
driver.find_element_by_xpath("(//button[#aria-pressed='false'])[1]").click()
But I get this error:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"(//button[#aria-pressed='false'])[1]"}
However, this exists in my HTML content, like:
<div class="webix_el_box" style="width:90px; height:26px">
<button aria-pressed="false" type="button" class="webix_img_btn" style="line-height:22px;">
<span class="webix_icon_btn fa-lock red" style="max-width:22px;">
</span> Read Only
</button>
</div>
I have also tried to use the XPath expression:
//button[#class='webix_img_btn']//child::span[#class='webix_icon_btn fa-lock red']
Both XPath expressions worked fine in Google Chrome.
In the website I am able to find the button using Ctrl + F and 'Read Only', can I use it in Selenium?
It is not working because of Selenium Driver is one page behind ajax call after large json data download.
To click on the element with text as Read Only you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.webix_el_box > button.webix_img_btn"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#class='webix_el_box']/button[#class='webix_img_btn' and contains(., 'Read Only')]"))).click()
Note: You have to add the following imports:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
References
You can find a couple of relevant discussions on NoSuchElementException in:
"selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element" while clicking a 'Next' button with Selenium
Selenium in Python: "NoSuchElementException: Message: no such element: Unable to locate element"
Try doing just
driver.implicitly_wait(300)
driver.find_element_by_xpath("//button[#aria-pressed='false']").click()
This will click on the first element that satisfies the requirements.

How to identify the nested element using Selenium and Python

I have a nested element possibly within <svg> that I can't seems to access
I tried using
driver.find_element(By.CSS_SELECTOR, 'button.login-fake-btn')
and
driver.find_element(By.CSS_SELECTOR, 'login-fake-btn')
and a few others.
HTML structure of nested svg:
<svg class="1">
<div id="2">
<div>
<div class="3">
<div class="4">
<li>
<button class="5" type="button" id="login-fake-btn">
...closing tags
Snapshot of HTML:
I have no success with xpath either.
Error:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"button.login-fake-btn"}
How do I get to a nested svg using a css selector (or xpath, but I understand css to be better)?
It's a <button> element and it's out of the <svg> tag and possibly moving forward you'd invoke click() on it. Hence to locate the element you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.login-btn.btn-shadow#login-fake-btn[data-testid='login-fake-btn']")))
Using XPATH:
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='login-btn btn-shadow' and #id='login-fake-btn'][#data-testid='login-fake-btn']")))
Note : You have to add the following imports:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
References
You can find a couple of relevant discussions on NoSuchElementException in:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element while trying to click Next button with selenium
selenium in python : NoSuchElementException: Message: no such element: Unable to locate element

How to handle colon (:) in the name when using Selenium in Python

I am trying to search a number in a web page (https://muisca.dian.gov.co/WebRutMuisca/DefConsultaEstadoRUT.faces).
I know the name of the input element is: "vistaConsultaEstadoRUT:formConsultaEstadoRUT:numNit", however when I try to find the element I got this error:
"NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"[name="vistaConsultaEstadoRUT:formConsultaEstadoRUT:numNit"]"}
(Session info: chrome=81.0.4044.138)"
This is what I have tried:
from selenium import webdriver
driver = webdriver.Chrome("C:\\Users\\jcherrerab\\Anaconda3\\Lib\\site-packages\\selenium\\webdriver\\chrome\\chromedriver.exe")
driver.get("https://muisca.dian.gov.co/WebRutMuisca/DefConsultaEstadoRUT.faces")
driver.find_element_by_name("vistaConsultaEstadoRUT:formConsultaEstadoRUT:numNit").send_keys("860003020")
Can you help me pls?
The name attribute of the <input> element contains the : character as in:
vistaConsultaEstadoRUT:formConsultaEstadoRUT:numNit
And : bears a special effect when used within a css-selectors. Hence your program fails to find the desired element and raises NoSuchElementException
Solution
To find the element you can use either of the following Locator Strategies:
Using css_selector:
driver.find_element_by_css_selector("input[name='vistaConsultaEstadoRUT:formConsultaEstadoRUT:numNit']")
Using xpath:
driver.find_element_by_xpath("//input[#name='vistaConsultaEstadoRUT:formConsultaEstadoRUT:numNit']")
Best practices
As you are invoking send_keys() ideally you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='vistaConsultaEstadoRUT:formConsultaEstadoRUT:numNit']"))).send_keys("860003020")
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#name='vistaConsultaEstadoRUT:formConsultaEstadoRUT:numNit']"))).send_keys("860003020")
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Browser Snapshot:
Reference
You can find a couple of relevant discussions in:
Selenium “selenium.common.exceptions.NoSuchElementException” when using Chrome
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element while trying to click Next button with selenium

Categories