Selenium cannot find element by ID nor xpath - selenium

I'm trying to write a script in python (3.7.3) to automate logging into a website by using Selenium for the first time. I practiced with some basic examples and went through the Selenium documentations. All good so far. But when I try it on a website of my own choice; things go wrong...
I'm managing to open up the login page, but whenever I try to get the element ID corresponding to the username field, I'm getting the "NoSuchElementException", indicating that the ID-name I'm using is supposedly incorrect. I'm getting the name by looking at the HTML code by right clicking in the username-box and using the inspect function. When this is not working, I'm trying to find it through xpath, but also without success. Can anyone point out why the element is not being recognized?
Python code
from selenium import webdriver
path = r"C:\Users\path\chromedriver.exe"
driver = webdriver.Chrome(path)
driver.get ("https://a website")
driver.find_element_by_id("login-username").send_keys(login)
driver.find_element_by_id("login-sign-in-button").click()
Error message
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="login-username"]"}
HTML code of the username field:
<input id="login-username" type="text" name="username" placeholder="Username" msd-placeholder="Username" class="margin-bottom form-control ng-pristine ng-empty ng-invalid ng-invalid-required ng-touched" ng-model="formData.username" dh-autofocus="" required="">
Looking for element with xpath. I've changed the "" brackets around the id by '' to avoid errors.
driver.find_element_by_xpath("//*[#id='login-username']").send_keys(login)
And finally I tried the long xpath
driver.find_element_by_xpath("/html/body/ui-view/ui-view/div/div[1]/div[1]/ui-view/div/div[1]/div/div[2]/form/div[1]/div/input").send_keys(login)
I'm honestly hitting a brick wall here. It's probably not helping my knowledge of HTML is practically non existing.
EDIT 1
Added wait function. Code works now
driver.get ("https://a website")
element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "login-username")))
driver.find_element_by_id("login-username").send_keys(login)

Answering to close this question.
As Alok pointed out, we need to wait for the webelement to get loaded completely before trying to access it.
driver.get ("https://a website")
element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "login-username")))
driver.find_element_by_id("login-username").send_keys(login)

Related

Auto-checkin script for united airlines

Trying to submit the check in form for United Airlines (https://www.united.com/en/us/checkin). I can't get the continue button to automate. The xpath is dynamic so is css selector. Can't find a name or id browser to find. How can I find this element? Never coded before so this is all I have been able to pickup from youtube
html for button
<button class="app-components-Button-styles__button--LbfHO app-components-Button-styles__tertiary--20H47 app-components-Button-styles__contained--2kXyi" type="submit">Continue</button>
Code so far:
from selenium import webdriver
browser = webdriver.Chrome('/Users/jeff/chromedriver/chromedriver')
browser.get('https://www.united.com/en/us/checkin')
browser.find_element('name', 'confirmationNumberModel.number').send_keys('AAAA1')
browser.find_element('name', 'confirmationNumberModel.lastName').send_keys('LastName')
browser.implicitly_wait(3)
Try to locate button by XPath:
browser.find_element(By.XPATH, '//button[.="Continue"]').click()
You can also call submit method from any element inside form node:
browser.find_element('name', 'confirmationNumberModel.lastName').submit()
P.S. Note that browser.implicitly_wait(3) should be called before locating elements
Given the HTML:
<button class="app-components-Button-styles__button--LbfHO app-components-Button-styles__tertiary--20H47 app-components-Button-styles__contained--2kXyi" type="submit">Continue</button>
All the classnames are dynamically generated. So we can't use them and have to look out for the static element attributes.
Solution
To click on Continue you can use either of the following locator strategies:
Using css_selector:
driver.find_element(By.CSS_SELECTOR, "div[title='Confirmation or eTicket number'] button[type='submit']").click()
Using xpath:
driver.find_element(By.XPATH, "//button[#type='submit' and text()='Continue']").click()

find specific element for button using selenium for web scraping

I am inspecting one button element from a web page using chrome driver and selenium. And the html code for the particular button is:
<div class="label text-left text-link link-blue text-
uppercase">Financial Statement Analysis <span class="count">(2)</span>
</div>
I have tried different element options like find element by name, xpath, link text etc. But none of them unable to locate the element.
What will be the element to locate the button. ?
try Xpath :
//span[contains(#class,'count') and text() = '(2)']
You can try with this css selector :
div.label.text-left.text-link.link-blue.text-.uppercase
To locate the element with text as Financial Statement Analysis (2) you can use the following solution:
Java Solution:
WebElement elem = driver.findElement(By.xpath("//div[#class='label text-left text-link link-blue text-uppercase'][contains(.,'Financial Statement Analysis')]"));

Unable to locate the login box by type attribute or xpath

HTML looks like following
<input class="text-input text-input-md" dir="auto" ng-reflect-klass="text-input" ng-reflect-ng-class="text-input-md" type="email" aria-labelledby="lbl-14" autocomplete="off" autocorrect="off" placeholder="" ng-reflect-type="email">
the code fails to find login box...tried by attribute
var email_xpath = "//*[type='email']"
then xpath
var email_xpath = "/html/body/ion-app/ng-component/ion-split-pane/ion-nav/page-login/ion-content/div[2]/ion-list/ion-item[1]/div[1]/div/ion-input/input"
var email = webDriver.findElement(By.xpath(email_xpath))
but still unable to get the element....
===============Updated===============
most of the solutions posted below works with selenium firefox driver. The issue was really with htmlunit driver that i was using in scala. Probably it cannot handle javascript properly. I changed it with firefox driver and your solutions works well. The application being tested is an Ionic app (angular), hence i will have to look for another headless solution later.
//*[type='email'] is not correct XPath. Try below instead:
//*[#type='email']
Note that type='email' predicate means child node with string value 'email':
<input>
<type>email</type>
</input>
While #type='email' means attribute type with value "email"
The previous answer is correct but You can try this also //input[#type='email']
The generic syntax is something like as mentioned below for xpath
// - means relative xpath, can be present anywhere inside DOM
tagName - means html tags like td,tr,span,br,input etc
#- denotes start of attribute name present inside html tag
value - actual attribute value present inside DOM
//tagName[#attribute='value']
You can use any XPath, as some are already mentioned by #Andersson and #zsbappa
some others are
//input[#class='text-input text-input-md' and #type='email']
//input[contains(#type,'email')]
Since you are using WATIR, you don't have to write xpath, write the below code, it would work.
b.text_field(type: "email").set "abc#gmail.com"

selenium webdriver find element in region

Working with automated testing, I have come across the following issue quite a lot of time: I want to find an element on the page, but the element has to be at a specific region of the page.
Take the following as an example:
I have a searchfield with type-ahead on the site. In my sidebar, I have the element I am seraching for (lets call it "Selenium"), but that is not the element I am interested in, I want to see if my type-ahead search is delivering the expected result when searching for "Selenium".
<body>
<header>
<searchfield>
<searchresults>
<a .... >Selenium</a>
<searchresults>
</searchfield>
</header>
<aside>
...
<a .... >Selenium</a>
...
</aside>
</body>
If I in selenium webdriver search for the linktext "Selenium" it will find both entries, the one in the sidebar aswell as the one in the searchfield.
Furthermore am I not able to wait for the searchresult with the WaitForVisible command on the linkText as Selenium will find the element in the sidebar and conclude that the element is preset.
So my question is:
"With selenium webdriver, how do I search for an element within a specific region?"
Poking around with this issue, I came across the use of Xpath. With this I could create "areas" where I want to search for an element. As an example, I went from
html/body/div/aside/div[2]/ul/li
to
//div[contains(#class,'coworkerwidget')]/ul/li
Now the code is MUCH more dynamic, and less prone to errors if our frontend guys edit something in the future.
Regarding the search, I could now set up something like the following code
//div[contains(#class, 'searchfield')]//div[contains(#title, 'searchfield') and contains(., '" + searchword + "')]"
First we specify that we want to look in the searchfield area:
//div[contains(#class, 'searchfield')]
I can then set some more criteria for the result I want to find:
//div[contains(#class, 'title') and contains(., '" + searchword + "')]
Some litterature on the subjects for further reading.
http://www.qaautomation.net/?p=388
http://www.w3schools.com/xsl/xpath_syntax.asp
Click a button with XPath containing partial id and title in Selenium IDE
Retrieve an xpath text contains using text()

Selenium webdriver testng, how to select this xpath/id

I am trying to find the correct Xpath in order to select the link of the HTML code below. I tried with id and also trying to catch the text with xpath but it doesn't work, I would appreciate some help.
<td>
<a id="2018" onclick="goToOperator(50210, 129216, 195481);">
<img src="/NexxarUtilWeb/images/logo/SWFS_lcc_small.jpg" alt="Logo">
<br>
2018 - LCC TRANS-SENDING LIMITED
</br>
</a>
</td>
Thank u in advance!
The correct locator would be:
WebElement myLink = driver.findElement(By.id("2018"));
If you really want to use an XPath you could use:
WebElement myLink = driver.findElement(By.xpath("//a[#id='2018']"));
Where the XPath is:
//a[#id='2018']
I suspect your real problem is that the element does not exist on the page when it is initially loaded but appears after a set amount of time, if that is your problem you want to use an explicit wait like this:
WebDriverWait waiting = new WebDriverWait(driver, 10, 100);
waiting.until(ExpectedConditions.visibilityOfElementLocated(By.id("2018"));
Of course you may not care about it being visible, you may just want it to be present:
WebDriverWait waiting = new WebDriverWait(driver, 10, 100);
waiting.until(ExpectedConditions.presenceOfElementLocated(By.id("2018"));
If none of this is helpful we are going to need some more detailed information from you about your problem, a stack trace showing the error you are getting would be useful as well.
In the above case we cant find the element by id(id is dynamically changing)
so, you can use tag name
options=driver.find_element_by_tag_name("br")
for option in options:
if("2018 - LCC TRANS-SENDING LIMITED" in option.text)
option.click()