I want to get URL's of hyperlinks present on current page using Selenium Web driver. Can anyone help.
To get the URL of all the links on a page, you can store all the elements with tagname 'a' in a WebElement list and then you can fetch the href attribute to get the link of each WebElement.
you can refer to the following code :
List<WebElement> links = driver.findElements(By.tagName("a")); //This will store all the link WebElements into a list
for(WebElement ele: links) // This way you can take the Url of each link
{
String url = ele.getAttribute("href"); //To get the link you can use getAttribute() method with "href" as an argument
System.out.println(url);
}
Just get them from the href attribute using getAttribute() (assuming you are in java):
WebElement link = driver.findElement(By.tagName("a"))
String url = link.getAttribute("href")
Related
I am using TestNG and selenium for testing web app.
<div class="infoMessage">The request has been successfully submitted.</div>
In the my TestNG class file, like any other HTML elments for div element also
I have
#FindBy(xpath="//*[#id='wrapper']/table/tbody/tr[1]/td/div")
WebElement resultdiv;
Now that I got the webelement in resultdiv, how can i read the content "The request has been successfully submitted" ?
Just at a quick glance, it seem like you can try use className instead xpath:
#FindBy(className="infoMessage")
WebElement resultdiv;
Use .getText(); to achieve:
String text = resultdiv.getText();
Hi #bnath002 You can use Contains text Xpath, below is the code. this code always work for text.
WebElement element = driver.findElement(By.xpath("//div[contains(text(),'The request has been successfully submitted.')]"));
String innerText= element.getText();
System.out.println("Your inner text is: "+innerText);
I'm a little unfamiliar with #FindBy, but i'm assuming you could use getText() as normal. But if everything went as planned and the xpath located your element successfully, you should be able to retrieve the text with the following :)
WebElement element = driver.findElement(By.xpath("//*[#id='wrapper']/table/tbody/tr[1]/td/div"));
String innerText= element.getText();
System.out.println("Your inner text is: "+innerText);
can any one help me for finding the correct xpath for the given link Logout
What you could do is to locate all links in your page, and then file the one corresponding to what you're searching for.
Here is the code converted to java
public static IWebElement GetLinkContainingText(string textToBeContained) {
// Here Driver is my ChromeDriver instance. You can replace a, by whatever tag your href is in.
ArrayList<WebElement> allTags = Driver.FindElements(By.Xpath("//a"));
for (WebElement v : allTags) {
if (v.GetAttribute(href).contains(textToBeContained)) {
return v;
}
}
return null;
}
calling the method would result for you in that.
WebElement elementYouSeachFor = GetLinkContainingText("http://ec2-34-210-163-161.us-west-2.compute.amazonaws.com:8094/login/index/logout");
Most likely
WebElement elementYouSeachFor = GetLinkContainingText("/logout");
would work too since there's probably not many links with logout on your page.
Hope this helps.
driver.findElement(By.xpath("//a[text()='Logout']"))
Is one way.
driver.findElement(By.linkText("Logout"));
Both could be problematic if you have more than 1 logout link on the page.
More resources on selecting elements: https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/By.html
I am new to selenium. I am trying to retrieve the value of a textbox. Below is my code.
WebElement e = driver.findElement(By.id("id"));
e.sendKeys("text");
String str = e.getAttribute("value");
System.out.println(str);
The above code is working fine in all sites but is not working for a particular site. I can't share the site details.
Any explanation regarding why the code is not working for a site or is there another way to get the text from a textbox?
getAttribute('value') will provide you with null cause html snippet doesnot contains value attribute or the DOM object of that element has no value attribute. Try getting the text with :
String str = e.getText(); //If it helps
OR
use JavascriptExecutor as
JavascriptExecutor js = (JavascriptExecutor) driver;
String str = js.executeScript(
"return document.getElementById('company').value")
.toString();
How would I verify the image is displaying is the correct path/name in Selenium using WebDriver?
I started using this code but not sure :
string _active = "<img style="display: ;" alt="Active" src="../App_Themes/Default/images/check.png"/>";
driver.FindElement(By.XPath("//*[#id='ctl00_ContentPlaceHolder1_AddeCardControl1_gv']/tbody/tr[11]/td[7]/img")).Text.Contains(_active);
I would like to clarify to you that this code
driver.FindElement(By.XPath("//*[#id='ctl00_ContentPlaceHolder1_AddeCardControl1_gv']/tbody/tr[11]/td[7]/img")).Text.Contains(_active);
does not give you the html code for the image tag but a IWebElement object. And you can read the various attributes of this WebElement by using the GetAttribute method.
You will have to get the src attribute of the img tag you are looking for by locating the image(webelement) by xpath and then
IWebElement element = driver.FindElement(By.XPath("Your xpath"));
string path = element.GetAttribute("src");
Now you can verify the path of your image. Hope this helps you.
How to verify whether links are present or not?
eg.
I have 10 links in a page, I want to verify the particular link
Is it possible?
I am using selenium with Java.
Does i can write inside the selenium code
eg
selenium.click("searchimage-size");
selenium.waitForPopUp("dataitem", "3000");
selenium.selectWindow("name=dataitem");
foreach(var link in getMyLinkTextsToTest())
{
var elementToTest = driver.findElement(By.linkText(link));
Assert.IsNotNull(elementToTest);
}
What you can do is find all links on the page like this:
var anchorTags driver.findElement(By.TagName("a"));
and then iterate through the anchorTags collection to make you you've got what you're looking for.
Or if you have a list of the link texts you can do something like this:
foreach(var link in getMyLinkTextsToTest())
{
var elementToTest = driver.findElement(By.linkText(link));
Assert.IsNotNull(elementToTest);
}
This code is all untested and right off the top of my head so you might need to do some slight modification but it should be close to usable.
if you are using Selenium 1.x you can use this code.
String xpath = "//<xpath till your anchor tag>a/#herf";
String href = selenium.getAttribute(xpath);
String expectedLink = "your link";
assertEquals(href,expectedLink);
I hope this may help you...
List<WebElement> links = driver.findElements(By.tagName("a"));
for(WebElement we : links) {
if("Specific link text".equals(we.getText("Specific link text"))) {
we.click();
}
}
I'm taking all links to List variable 'links' and iterating it. Then checking condition, for the specific text we looking in the link is presenting in the list or not. If it found out, it'll click on it
If you're looking to verify each specific for the content of href, you can use javascript to return the outerHTML for a specific Webelement which you can identify however you like; in the example below I use By.cssSelector:
WebElement Element = driver.findElement(By.cssSelector("..."));
String sourceContents = (String)((JavascriptExecutor)driver).executeScript("return arguments[0].outerHTML;", element);
assertEquals(sourceContents, "Learn More");
If you want to make it a tad more elegant you can shave the undesired elements off of the string, but this is the general case as of Selenium-java: 2.53.1 / Selenium-api: 2.47.1 as I can observe.
Best approach would be to use getText() method
List<WebElement> allLinks = driver.findElements(By.tagName("a"));
for(WebElement specificlink : allLinks ) {
if(specificlink.getText().equals("link Text"){
//SOPL("Link found");
break;
}
}