Python/Selenium Identifying an element issue - python

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

Related

Trying to fill out an online form using selenium but it can't find the element

I am new to selenium and have tried searching for this element using xpath, ID, and class, however I cannot seem to find it. Upon using inspect element, the element is certainly there.
<input type="email" class="chakra-input rr-auth-web-user-email-input css-1wpvo2" placeholder="Email" name="emailAddress" id="emailAddress" required="" aria-required="true">
I have tried
login = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[#id='emailAddress']"))).click()
Adjusting for ID, class name, etc, but the code can't seem to find it.
I am expecting the code to find the element and clicking on it, I have done this before on other sites however I cannot seem to figure out why it is not working here.
Given the HTML:
<input type="email" class="chakra-input rr-auth-web-user-email-input css-1wpvo2" placeholder="Email" name="emailAddress" id="emailAddress" required="" aria-required="true">
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, "input#emailAddress[name='emailAddress']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#id='emailAddress' and #name='emailAddress']"))).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
Your mistake is that you're defining an event as a variable (the click() event, assigned to the login variable).
You need to assign the actual element to the variable, as:
login = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[#id='emailAddress']")))
And now you should be able to click on it:
login.click()
Also, this click should happen without assigning the element to a variable, by simply doing:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[#id='emailAddress']"))).click()
Selenium documentation can be found here.

Selenium unable to locate xpath however the xpath is there in the browser

Im not sure if that was quite the best title but Im not sure how else to describe it, I am trying to use selenium to automate 2fa on a website so all I need to do is anwser the phone call and the script will take care of the rest however the button I am trying to get selenium to click keeps showing up as unable to locate even though its always in the same place and never changes here is my code in python
callMe = driver.find_element('xpath', '//*[#id="auth_methods"]/fieldset/div[2]/button')
callMe.click()
sleep(25)
this is one of three buttons that all have the same element information besides the xpaths here are all 3 button elements I am trying to grab the second one
<button tabindex="2" type="submit" class="positive auth-button"><!-- -->Send Me a Push </button>
<button tabindex="2" type="submit" class="positive auth-button"><!-- -->Call Me </button>
<button tabindex="2" type="submit" class="positive auth-button"><!-- -->Text Me </button>
I am not sure how else I can locate the second button besides using the xpath but that is not working and I dont know if I can or how I can search for the button based off the text that is inside it.
Did you try with By ?
from selenium.webdriver.common.by import By
callMe = driver.find_element(By.XPATH, '//*[#id="auth_methods"]/fieldset/div[2]/button')
Try using By.cssselector, get the css selector of the main body html for the button you want.
callMe = driver.find_element(By.css_selector, 'selectorofbodyhtml')
callme.click()
Try below xpath
//button[starts-with(text(),'Call Me')]
The desired element Call Me is a dynamic element, so to click on it you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using XPATH and contains():
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(., 'Call Me')]"))).click()
Using XPATH and starts-with():
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[starts-with(., 'Call Me')]"))).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

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"));

How to click on the ember.js enabled button using Selenium and Python

I have been trying to make this clickable and I just cannot understand what I am doing wrong.
I am also trying to induce webdriverwait, so that it is clicked when it appears.
This is my code so far:
def order(k):
driver = webdriver.Chrome(os.getcwd()+"\\webdriver\\chromedriver.exe")
driver.get("website.com/login-to-checkout")
driver.find_element_by_id('i0116').send_keys(k["email"])
driver.find_element_by_id('i0118').send_keys(k["password"])
driver.find_element_by_id('idSIButton9').click()
delay()
#sign in button
driver.find_element_by_id('idSIButton9').click()
#Button below I cant get to be clicked
with webdriver.Chrome() as driver:
wait = WebDriverWait(driver, 7)
wait.until(presence_of_element_located((By.CSS_SELECTOR, "#ember1053")))
driver.find_element(By.id, "ember1053").click()
this is the source code for the button that I am trying to make clickable:
<div id="ember1037" class="btn-group m-b-lg m-t-lg order-call-to-action ember-view"><!----> <!--biBehavior 80 means place order Place Order-->
<button aria-live="polite" type="button" tabindex="0" data-m="{"aN":"shoppingCart","cN":"PlaceOrder","bhvr":80}" id="ember1053" class="btn theme-default btn-primary cli-purchase ember-view"><!----> Place order
</button></div>
The desired element is an Ember.js element and the value of the id attribute of the <button> will keep changing dynamically, every time you access the AUT(Application Under Test). Hence to click() on 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:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn.theme-default.btn-primary.cli-purchase.ember-view[id^='ember'][type='button'][aria-live='polite']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='btn theme-default btn-primary cli-purchase ember-view' and starts-with(#id,'ember')][contains(., 'Place order') and #aria-live='polite']"))).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 detailed discussions in:
Selenium - Finding element based on ember
Ember dropdown selenium xpath
This may help but I've had issues with webdriver not clicking on a button when I use the id to find it.
The work around I've found is using the xpath instead of the id.
Like this, it's worth a try.
driver.find_element_by_xpath("""//*[#id="submit-button"]""").click()

Selenium won't locate element with any method

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

Categories

Resources