How to handle colon (:) in the name when using Selenium in Python - 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

Related

How to click a javascript button via Selenium in python

I am trying to pull some quotes on a website. To get the quote you have to click the Javascript button below:
Below is my python code to click the button:
driver = webdriver.Edge()
driver.get("http://generatorland.com/glgenerator.aspx?id=86")
driver.find_element(By.XPATH,"//a[#href='JavaScript:void(0);']").click()
However, when I run the code to test the button press, it throws an error:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//a[#href='JavaScript:void(0);']"}
(Session info: MicrosoftEdge=110.0.1587.46)
Why can't it find the button, even when I specify the href sample for the XPATH to find?
You can move a step deeper and use the <img> tag.
Solution
The desired element is a dynamic element, so to click on the clickable 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, "a > img[alt='Confucius']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a/img[#alt='Confucius']"))).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

Having trouble locating an element with in an iframe using Selenium Python

I am attempting to click a button using an XPath but I am unable to locate the element. Complete noob here.
Here is the button element I copied:
<button _ngcontent-ygw-c218="" class="btn"><span _ngcontent-ygw-c218="" translate="">SHOW ALL</span></button>
Here is my programming:
ShowAll =driver.find_element_by_xpath('//*[#id="app-SelectComponents"]/div[1]/button[1]')
ShowAll.click()
I have tried the following solution I found online to no avail (I've also replaced the 'btn' with 'SHOW ALL', no luck there):
driver.switch_to.frame(driver.find_element_by_name('btn'))
ShowAll =driver.find_element_by_xpath('//*[#id="app-SelectComponents"]/div[1]/button[1]')
ShowAll.click()
driver.switch_to.default_content()
Much appreciated.
EDIT: Here is a picture for reference. What am I doing wrong or what I can do to work around this issue?
You need to add time to make sure the element is available before asking selenium to pick it.
Try this:
import time
driver.switch_to.frame(driver.find_element_by_name('btn'))
time.sleep(5) #you can change the 5 depending on the number that works
ShowAll =driver.find_element_by_xpath('//*[#id="app-SelectComponents"]/div[1]/button[1]').click()
driver.switch_to.default_content()
The element with the text as SHOW ALL is within an <iframe> so you have to:
Induce WebDriverWait for the desired frame to be available and switch to it.
Induce WebDriverWait for the desired element to be clickable.
You can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#iParts")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn > span[translate]"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#id='iParts']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='btn']/span[text()='SHOW ALL']"))).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
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

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

Python XPath selector not working with selenium

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

selenium.common.exceptions.TimeoutException: Message error sending text to username field within iframe using Selenium Python

When i try to locate one element with selellium it fails
driver = webdriver.Chrome(executable_path = r'./chromedriver.exe')
driver.get("http://eltiempo.com/login")
try:
element = WebDriverWait(driver, 30).until(
EC.presence_of_element_located((By.XPATH, "//*[#id='username']"))
)
finally:
driver.quit()
To send a character sequence to the username field as the element is within an <iframe> so you have to:
Induce WebDriverWait for the desired frame to be available and switch to it.
Induce WebDriverWait for the desired element to be clickable.
You can use either of the following Locator Strategies:
Using CSS_SELECTOR:
driver.get("https://www.eltiempo.com/login")
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe.iframe-login")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='username']"))).send_keys("Nick Rondon#stackoverflow.com")
Using XPATH:
driver.get('https://www.eltiempo.com/login')
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#class='iframe-login']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#name='username']"))).send_keys("Nick Rondon#stackoverflow.com")
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:
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

Categories