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()
Related
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)
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')]"));
I have the following element:
<div class="ui-helper-hidden" id="shell.indicator.busy">
<label>Processing...</label>
<br />
<img src="/RightCrowd/Images/loading.gif" alt="" />
</div>
And it appears like this:
I would like to wait for it to appear and then to disappear and continue with accessing elements. This appears on most pages of the web application. We've tried a few things but sometimes it is visible by Selenium and sometimes is not, although I can see it with my eyes.
I believe that this processing image appears on top of the current page and using the current handler may be useless. Not sure.
We've tried something like this:
WebElement element = (new WebDriverWait(webDriver, 10))
.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//img[contains(#src,'/Images/loading.gif')]")));
boolean processingEnd = (new WebDriverWait(webDriver, timeoutWaitForProgressbar))
.until(ExpectedConditions.invisibilityOfElementLocated(By.id("shell.indicator.busy")));
So we've tried both xpath and id... Please let me know what's the best way to handle this situation.
By default, the WebDriverWait would check the Expected Condition status every half a second. I would try to issue the expected condition check requests more often with a FluentWait class:
Wait wait = new FluentWait(driver)
.withTimeout(timeoutWaitForProgressbar, SECONDS)
.pollingEvery(100, MILLISECONDS);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("shell.indicator.busy")));
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()
Is it possible to use linkText locator in this code
I used driver.findElement(By.linkText("welcome")).click();
But it didn't work.
Please help....
<div class="back-to">
<a class="button blue" href="javascript:history.back()">welcome</a>
</div>
The linkText should work in this case. Or else try the below alternatives(and please provide sufficient implicit timeout to give selenium sufficent time to detect the element):
1. Using xpath, to click on the element 'a' with exact innerHTML/text as 'welcome':
driver.findElement(By.xpath("//a[.='welcome']")).click();
2- Using JavascriptExecutor to click on the element with exact innerHTML/text as 'welcome':
((JavascriptExecutor)driver).executeScript("arguments[0].click();", driver.findElement(By.xpath("//a[.='welcome']")));
3- Using partialLinkText to click on the link with partial text 'welcome'
driver.findElement(By.partialLinkText("welcome")).click();
This Should be enough:
driver.find_element_by_xpath('//a[#class="button blue"]').click();