Robot Framework Locator not able to find locator - selenium

I have been trying to find the locator for my testing in robotframework. Below is my html element block and I am unsure of what is the "stable" locator and test on this. I would sincerely appreciate the xpath or any stable locator.
<div class="sc-jXcwIi bnswfh cosmos-layout-child" data-cosmos-key="stack-layout.item"><div data-cosmos-key="row-layout" class="sc-bQCGiA fFRDIw"><div class="sc-fXgAFM cosmos-layout-child" data-cosmos-key="row-layout.item">deploy-extension</div><div class="sc-fXgAFM cosmos-layout-child" data-cosmos-key="row-layout.item"><span data-cosmos-key="text" type="allcaps" class="sc-fujyUd cWWTbP">MACHINE TO MACHINE</span></div></div></div>
I have tried the locator
Click Element xpath://div[contains(text(), "deploy-extension")]
But this is giving me error.
export existing tenant | FAIL |
ElementClickInterceptedException: Message: element click intercepted: Element <div class="sc-fXgAFM cosmos-layout-child" data-cosmos-key="row-layout.item">...</div> is not clickable at point (431, 355). Other element would receive the click: <p data-cosmos-key="paragraph" class="sc-dlnjPT corHA">...</p>
(Session info: headless chrome=87.0.4280.66)

focus on the element first.
Set Focus To Element xpath://div[contains(text(), "deploy-extension")]

you can try with various locators as below,
//div[contains(text(), "deploy-extension")]//following::span[contains(text(),'MACHINE TO MACHINE')]
//span[#text='allcaps'and contains(text(),'MACHINE TO MACHINE')]
https://www.tutorialspoint.com/what-is-xpath-in-selenium-with-python

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

Selenium and LINK_TEXT

I tried to scrapp a cost with a LINK TEXT but my scrapping method can't find the TEXT:
budget = budgets.append((driver.find_element(By.LINK_TEXT, 'Total budget/expenditure:')).text)
The web site is : https://keep.eu/projects/12880/A-la-d-couverte-des-plus-be-EN/
It works with the XPATH but i need to scrap many page like this one and sometimes the To total budget/expenditure and European Union Funding was not exactly at the same place.
The error is that :
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"link text","selector":"Total budget/expenditure:"}
(Session info: chrome=100.0.4896.127)
I don't know why sometimes I can used the LINK-TEXT and sometimes no.
I tried PARTIAL_LINK_TEXT too but it can't works.
Link Text
A linkText is used to identify the hyperlinks on a web page. It can be determined with the help of an anchor tag <a>.
But your desired element is within a <strong> tag, so By.LINK_TEXT won't work here.
<p>
<strong>Total budget/expenditure: </strong>
" EUR 313 300.00"
</p>
Solution
To locate the element you can use either of the following locator strategies:
Using xpath and contains():
element = driver.find_element(By.XPATH, "//p//strong[contains(., 'Total budget/expenditure:')]")
Using xpath and starts-with():
element = driver.find_element(By.XPATH, "//p//strong[starts-with(., 'Total budget/expenditure:')]")

Selenium cannot find element by ID nor xpath

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)

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

Selenium WebDriver - Find Element

I've gone through the Selenium Documentation for locating elements, but I can't seem to figure out how to find the element in my code.
Here is my code from my .cshtml:
<a onclick="alter('#key', '#value')" href="#edit" id="#key-display">#value</a>
I am trying to locate and click the #value at the end.
Here is what it looks like when I inspect the value on Chrome:
<a onclick="alter('February 9, 2018', '1.00000')" href="#edit" id="February 9, 2018-display">1.00000 gallons</a>
I am able to locate the element by link text like this:
chromeDriver.FindElementByLinkText("1.00000 gallons").Click();
However, the link text will change constantly and I want to be able to locate it after it changes.
I have tried locating by several ways:
chromeDriver.FindElementByLinkText("#value").Click();
chromeDriver.FindElementByXPath("//a[#id='#key-display']").Click();
chromeDriver.FindElementById("#key-display").Click()
You will have to locate the element by the HTML in the page after it's rendered so the cshtml variable name can't be used. Having said that, you should be able to find a locator that will work. I would start with a CSS selector like
a[href='#edit']
That should work unless you have multiple edit links on the page. If that doesn't work, I would try
a[href='#edit'][id$='-display']
To find the element and invoke click() on the element you can use either of the following Locator Strategies :
xpath (where ID contains -display and href is #edit)
"//a[contains(#id,'-display') and #href='#edit']"
You can be more granular adding the onclick attribute as :
"//a[contains(#id,'-display') and #href='#edit' and starts-with(#onclick,'alter')]"
cssSelector (where ID ends with -display and href is #edit)
"//a[id$='-display'][href='#edit']"
You can be more granular adding the onclick attribute as :
"//a[id$='-display'][href='#edit'][onclick^='alter']"