How to selenium on python [duplicate] - selenium

This question already has answers here:
Selenium "selenium.common.exceptions.NoSuchElementException" when using Chrome
(1 answer)
"selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element" while clicking a 'Next' button with Selenium
(1 answer)
Selenium in Python: "NoSuchElementException: Message: no such element: Unable to locate element"
(5 answers)
Closed last year.
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//input[#name='btn_aplist_export'][#type='button']"}
<div id="btn_area">
<input type="button" name="btn_aplist_export" id="btn_aplist_export" bt="btn71" value="AP List Export"
onclick="run_aplist_export();" class="input_normal hiding"
style="float:left;margin-right:10px;" adflag="1" swflag="1"
autocomplete="off">

Related

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

Robot Framework Locator not able to find locator

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

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)

How to retrieve text value from input-tag element? [duplicate]

This question already has answers here:
Using Selenium WebDriver to retrieve the value of an HTML input
(10 answers)
Closed 6 years ago.
I need to retreive phone number from the UI element which looks like this:
And here is the HTML-code of this element:
<label class="card__phone input">
<input class="input__text ng-pristine ng-untouched ng-valid ng-valid-mask ng-not-empty" ng-model="person.msisdn" ui-mask="+9 (999) 999-9999" disabled="" placeholder="+_ (___) ___-____" type="text">
</label>
As you can see, we do not have a phone number data in this element, just format. I've tried to find the number using .//*[contains(text(), '+7 (921) 222-3344')] XPATH search - no results.
Forntend engeneers use Angular JS to build current web-app.
Well, the answer is to getAttribute("value"), from the WebElement.

Selenium wait not solving error "Element is not currently visible"

I got an error:
Element is not currently visible and so may not be interacted with
Command duration or timeout: 63 milliseconds
So I added "wait" object to wait for the html obj to load:
val wait: WebDriverWait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.id("company_id")))
val selectCompany = new Select(driver.findElement(By.id("company_id")))
selectCompany.selectByValue("975")
but still i get the error:
Exception in thread "main" org.openqa.selenium.TimeoutException: Timed
out after 10 seconds waiting for element to be clickable: By.id:
company_id
and I dont get it since the html is:
<select class="upload_company_id jcf-hidden" id="company_id" name="company_id" size="2"><option value="">Select account...</option>
<option value="100">100</option>
<option value="101">101</option>
<option value="104">104</option>
<option value="975">105</option>
this is more of the html:
thanks
I am not sure that the expected condition you choosed is right.The class you are using is select class. Could you please try elementToBeSelected(WebElement element)?
What you need to check is the computed CSS values of the element you are clicking. (In Chrome web inspector select the Computed tab on the right).
Selenium thinks that an element is hidden and can't be interacted with if any of these CSS values are set for the element:
display=none or
visibility=hidden or
visibility=collapse
If you can find any of these values on the computed tab, you will have to use JS to click on the element, or change the CSS value of the element on some way - Selenium will refuse interacting with the element regardless of the actual visibility of it. (I suspect the problem will be the "jcf-hidden" class)
When inspecting the element, switch to this tab in Chrome inspector on the right side:
and look for any of the mentioned values in the list.