Selenium won't locate element with any method - python

I'm having trouble having selenium locate an element on a website (gleam to be exact). Ideally I would like the driver to send keys to an input field, but selenium won't locate it for some reason.
I've already tried locating by ID, Xpath and by name. Any suggestions on how to locate this element?
Here's the html:
<input
id="contestant[name]"
name="name"
ng-model-options="{ debounce: 300 }"
ng-model="contestantState.form.name"
ng-pattern=".*"
placeholder="Alice Smith" required=""
style="width: 246px"
type="text"
class="ng-empty
ng-invalid
ng-invalid-required
ng-valid-pattern
ng-dirty
ng-valid-parse
ng-touched"
>

Try one of these
By.CssSelector("[id*='contestant']")
By.CssSelector("[ng-model='contestantState.form.name']")
By.CssSelector("[name='name']")

Use WebDriverWait to handle the dynamic elements on Web Page.Try following code.
If this code not work check whether your input element inside any iframe.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium import webdriver
inputelement=WebDriverWait(driver,40).until(expected_conditions.element_to_be_clickable((By.ID,'contestant[name]')))
inputelement.send_keys("Apple")

To send a character sequence to the desired element as the element is an Angular element so 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(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.ng-empty.ng-invalid.ng-invalid-required.ng-valid-pattern.ng-dirty.ng-valid-parse.ng-touched[id=\"contestant[name]\"]"))).send_keys("ml2017")
Using XPATH:
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#class='ng-empty ng-invalid ng-invalid-required ng-valid-pattern ng-dirty ng-valid-parse ng-touched' and #id=\"contestant[name]\"]"))).send_keys("ml2017")

Related

How to find element using Selenium and Python

Just starting to learn selenium using python and running into trouble with the basics of finding elements. This is a search/text box that I am trying to click into and send text to it.
Here is the code of the element I got from Edge.
<div class="box" id="ClinicSrch" style="display:block">
<span align="lb">Last:</span>
<input type="search" name="NameLast" size="15" class="white1" value="" maxlength="25">
First: <input type="search" name="NameFirst" size="15" class="white1" value="" maxlength="25"><br>
Here is my code:
driver.find_element(By.XPATH, '//*[#id="ClinicSrch"]/input[1]').click()
The xpath that I get when copying the element from edge inspect tool is:
//*[#id="ClinicSrch"]/input[1]
Full xpath is:
/html/body/form/div[1]/div[1]/input[1]
I've been trying with other fields and buttons where I have similar xpaths but nothing is working. I always get no such element.
To locate the Last Name search field 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#PtSrchFrame[name='PtSrchFrame']")))
element = driver.find_element(By.CSS_SELECTOR, "input[name='NameLast']")
Using xpath:
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#id='PtSrchFrame' and #name='PtSrchFrame']")))
element = driver.find_element(By.XPATH, "//input[#name='NameLast']")
Ideally, to locate 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.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#PtSrchFrame[name='PtSrchFrame']")))
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='NameLast']")))
Using XPATH:
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#id='PtSrchFrame' and #name='PtSrchFrame']")))
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#name='NameLast']")))
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

How to insert a value in text box using Python Selenium

I have the following HTML structure and I am trying to use Selenium to enter a value
<div class="form-group justify-content-center d-flex">
<form id="main-form" class="form-inline" action="/pushData" method="post">
<input type="hidden" name="_token" value="bnePp0JmVaVYuaTIAfuVIGT2y7usVssX3vQrAGaz">
<input type="text" id="input-url" class="url-input" name="url" placeholder="Paste URL to shorten">
<button class="button main-btn main-btn_primary" id="button-submit">Cut</button>
</form>
</div>
Here is my code
driver.find_element_by_id("input-url").send_keys("test")
driver.find_element_by_id("button-submit").click()
I want to get this element and enter a value.
Select element by id:
inputElement = driver.find_element_by_id("input-url")
inputElement.send_keys('testing')
Now you can simulate hitting ENTER:
inputElement.send_keys(Keys.ENTER)
or if it is a form you can submit:
inputElement.submit()
Selenium 4.3.0: Deprecated find_element_by_* and find_elements_by_*
are now removed (#10712)
The following code will work:
[..]
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
[..]
WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID, 'input-url'))).send_keys('test')
button_to_be_clicked = browser.find_element(By.ID, 'button-submit')
button_to_be_clicked.click()
To send a character sequence to the <input> element you can use either of the following locator strategies:
Using css_selector:
driver.find_element(By.CSS_SELECTOR, "input.url-input#input-url[name='url'][placeholder='Paste URL to shorten']").send_keys("Phonex")
Using xpath:
driver.find_element(By.XPATH, "//input[#class='url-input' and #id='input-url'][#name='url' and #placeholder='Paste URL to shorten']").send_keys("Phonex")
Ideally to send a character sequence to the <input> 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.url-input#input-url[name='url'][placeholder='Paste URL to shorten']"))).send_keys("Phonex")
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#class='url-input' and #id='input-url'][#name='url' and #placeholder='Paste URL to shorten']"))).send_keys("Phonex")
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

How to send text into an address bar which has a data-dojo-attach-point attribute

I'm trying to enter text into an address bar using the following code in python:
driver.find_element_by_xpath('//*[#id="esri_dijit_Search_0_input"]').send_keys('text')
However, it does not register anything when running without debugging. Could you please provide any solutions on how to input text into an address bar with the following html?
<form data-dojo-attach-point="formNode">
<input maxlength="128" autocomplete="off" type="text" tabindex="0" class="searchInput" value="" aria-haspopup="true" id="esri_dijit_Search_0_input" data-dojo-attach-point="inputNode" role="textbox" placeholder="Address or Grid Reference" title="Address or Grid Reference" style="width: 170px;">
</form>
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.searchInput[id^='esri_dijit_Search'][title='Address or Grid Reference']"))).send_keys("text")
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#class='searchInput' and starts-with(#id, 'esri_dijit_Search')][#title='Address or Grid Reference']"))).send_keys("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

Python/Selenium Identifying an element issue

im trying to log in with python into webpage, but for some reason i cant get correct login element no matter what i try, because im noob and this is my first attempt.
This is supposed to be the element from webpage:
<label class="">Username</label>
<input type="text" formcontrolname="username" class="form-control ng-pristine ng-invalid ng-touched xh-highlight">
I have tried search by label without success:
username = browser.find_element_by_xpath("//label[contains(text(),'Username')]")
I have tried search by class:
username = browser.find_element_by_xpath('//input[#class="form-control ng-pristine ng-invalid ng-touched"]')
I have tried search with CssSelector:
username = browser.find_element_by_css_selector("input[formcontrolname='Username']")
I have even tried full road:
username = browser.find_element_by_xpath("/html/body/app/div[#class='app-container']/ng-component/div[#class='container']/div[#class='row']/div[#class='col']/div[#class='card']/ng-component/div[#class='card-body']/form[#class='ng-pristine ng-invalid ng-touched']/div[#class='form-group'][1]/input[#class='form-control ng-pristine ng-invalid ng-touched']")
I don't know what i'm doing wrong, please help
To locate 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:
username = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[formcontrolname='username']"))).click()
Using XPATH:
username = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#formcontrolname='username']"))).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
There are several possible issues:
You may be missing a wait / delay to let the page loaded before you try accessing the elements.
The element may be located inside an iframe, so you will have to switch to that iframe in order to access the elements inside it.
The locator may be not unique.
To give more clear answer we need to see the we page you are working with and all your relevant code

Trying to select an input box through selenium python but the 'id' in the element changes every time I reload the page

HTML:
<input id="4432e17d-eed6-4620-9bb4-d75ffbd321fa" type="email" placeholder="Email address" value="" name="emailAddress" data-componentname="emailAddress" autocomplete="email" autocorrect="off" autocapitalize="off" spellcheck="false">
Refreshed:
<input id="4a463943-7f58-42ea-9317-ba9f9518811e" type="email" placeholder="Email address" value="" name="emailAddress" data-componentname="emailAddress" autocomplete="email" autocorrect="off" autocapitalize="off" spellcheck="false">
How can I select this element without using ID.
I've tried by XPATH and CSS SELECTOR both haven't worked.
Full XPATH:
/html/body/div[2]/div[3]/div[5]/form/div[1]/input
XPATH:
//*[#id="4a463943-7f58-42ea-9317-ba9f9518811e" except the ID changes.
Any help?
To send a character sequence to the Email address field you can use either of the following Locator Strategies:
Using css_selector:
driver.find_element(By.CSS_SELECTOR, "input[name='emailAddress'][data-componentname='emailAddress']").send_keys("sebtheoo#stackoverflow.com")
Using xpath:
driver.find_element(By.XPATH, "//input[#name='emailAddress' and #data-componentname='emailAddress']").send_keys("sebtheoo#stackoverflow.com")
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[name='emailAddress'][data-componentname='emailAddress']"))).send_keys("sebtheoo#stackoverflow.com")
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#name='emailAddress' and #data-componentname='emailAddress']"))).send_keys("sebtheoo#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
Try selecting element by Tag name
input_box = driver.find_elements_by_tag_name('tag_name_of_input_box')
or
since it has a name attribute so you can try that as well
input_box = driver.find_element_by_name('emailAddress')
or as it also has a place holder so even you can combine the xpath with placeholder
input_box = driver.find_element_by_xpath('"//input[#placeholder='Email address']"')
You need to check for tags that don't change. For example, in both the data, name is not changing. Hence you can move ahead with that.
To access element using name tag, try this syntax:
driver.findElement(By.name("emailAddress"));

Categories

Resources