Selenium. How to navigate element who's href contains a certain string? [duplicate] - selenium

The following is a bunch of links <a elements. ONLY one of them has a substring "long" as a value for the attribute href
<a class="c1" href= "very_lpng string" > name1 </a>
<a class="g2" href= "verylong string" > name2 </a> // The one that I need
<a class="g4" href= "very ling string" > name3 </a>
<a class="g5g" href= "very ng string" > name4 </a>
...................
I need to click the link whose href has substring "long" in it. How can I do this?
PS: driver.findElement(By.partialLinkText("long")).click(); // b/c it chooses by the name

I need to click the link who's href has substring "long" in it. How can I do this?
With the beauty of CSS selectors.
your statement would be...
driver.findElement(By.cssSelector("a[href*='long']")).click();
This means, in english,
Find me any 'a' elements, that have the href attribute, and that attribute contains 'long'
You can find a useful article about formulating your own selectors for automation effectively, as well as a list of all the other equality operators. contains, starts with, etc... You can find that at: http://ddavison.io/css/2014/02/18/effective-css-selectors.html

use driver.findElement(By.partialLinkText("long")).click();

You can do this:
//first get all the <a> elements
List<WebElement> linkList=driver.findElements(By.tagName("a"));
//now traverse over the list and check
for(int i=0 ; i<linkList.size() ; i++)
{
if(linkList.get(i).getAttribute("href").contains("long"))
{
linkList.get(i).click();
break;
}
}
in this what we r doing is first we are finding all the <a> tags and storing them in a list.After that we are iterating the list one by one to find <a> tag whose href attribute contains long string. And then we click on that particular <a> tag and comes out of the loop.

With the help of xpath locator also, you can achieve the same.
Your statement would be:
driver.findElement(By.xpath(".//a[contains(#href,'long')]")).click();
And for clicking all the links contains long in the URL, you can use:-
List<WebElement> linksList = driver.findElements(By.xpath(".//a[contains(#href,'long')]"));
for (WebElement webElement : linksList){
webElement.click();
}

Related

xpath selenium multi/and operators

Could anybody help how to solve this dilemma
I have this code:
<div>
Link
<span class="make">Chevrolet</span><br>
<span class="year">1956</span><br>
<span class="price">$20,000</span><br>
</div>
<div>
Link
<span class="make">Ford</span><br>
<span class="year">1958</span><br>
<span class="price">$21,000</span><br>
</div>
I need get the link for example Fords with the year greater then 1950.
Presently, I am using following xpath:
//*[text()='Ford' and .//text()>'1950']//parent::a
And this doesn't work! Have you any idea ?
This is one possible XPath :
//div[span/text()='Ford' and span/text()>1950]/a
Basically the XPath check if div has child span with text equals 'Ford' and another child span with value greater than 1950. Then from such div that match the two criteria above, return child a element.
demo
Better yet, only check span with class 'make' for manufacturer and span with class 'year' for manufacturing year :
//div[span[#class='make']='Ford' and span[#class='year']>1950]/a
You can write a generic method to get the required links, as shown below:
public static List<WebElement> getLinks(String linkText, int year) {
List<WebElement> links = driver.findElements(By.xpath("//a[text()='" + linkText + "']/following-sibling::span[1]"));
List<WebElement> linksGreaterThanRequiredYear = new ArrayList<WebElement>();
for (WebElement link : links) {
if (Integer.parseInt(link.getText()) > year)
linksGreaterThanRequiredYear.add(link);
}
return linksGreaterThanRequiredYear;
}
Hence, if you want to get the Fords with year greater than 1950, you can call above method in following way:
List<WebElement> fordsWithYearGreaterThan1950 = getLinks("Ford", 1950);
Above method can be further enhanced to include less than criteria as well. Let me know, if you have any further queries.

Locating Element with same class in Selenium using c#

I am trying to access ABC. I know that simple By.ClassName("bb") will not work here. How else can I access this content.
<body>
<div id="Frame">
<div class="bb"></div>
<div class="bb">ABC</div>
</div>
</body>
You can use the below css selector to get the value of "ABC".
.bb:nth-child(2)
You can use "XPath" Expression to find or locating your element.
Example : element = findElement(By.xpath("Your xpath expression");
For your XML use following line.
element = findElement(By.xpath("/body/div/div[#class='bb'][node()]");
There is a way to do this in the search using XPath but I am not an XPath expert. I can give you a solution using CSS Selectors. Basically you grab all the DIVs with class bb and then search their text to find the desired text.
String searchText = "ABC";
IReadOnlyCollection<IWebElement> divs = driver.FindElements(By.CssSelector("div.bb"));
foreach (IWebElement div in divs)
{
if (div.Text == searchText)
{
break; // exit the for and use the variable 'div' which contains the desired DIV
}
}

Using Selenium to select text

Want to select the text "This is for testing selector" from below HTML code.
<div class="breadcrumb">
<a title=" Home" href="http://www.google.com/"> Home</a>
<span class="arrow">»</span>
<a title="abc" href="http://www.google.com/">test1</a>
<span class="arrow">»</span><a title="xyz" href="http://www.google.com/">test2</a>
<span class="arrow">»</span>
This is for testing selector
</div>
I'm not sure if there an easy way out for this or not. It turned out to be more difficult than I thought. Below mentioned code is tested locally and giving correct output for me ;)
String MyString= driver.findElement(By.xpath("//div[#class='breadcrumb']")).getText();
//get all child nodes of div parent class
List<WebElement> ele= driver.findElements(By.xpath("//div[#class='breadcrumb']/child::*"));
for(WebElement i:ele) {
//substracing a text of child node from parent node text
MyString= MyString.substring(i.getText().length(), MyString.length());
//removing white spaces
MyString=MyString.trim();
}
System.out.println(MyString);
Let me know if it works for you or not!
Try with this example :
driver.get("http://www.google.com/");
WebElement text =
findElement(By.className("breadcrumb")).find("span").get(1);
Actions select = new Actions(driver);
select.doubleClick(text).build().perform();
I suggest also that you copy the xpath for the text you need and put it here to have the exact xpath
You cannot select text inside an element using xpath.
Xpath can only help you select XML elements, or in this case, HTML elements.
Typically, text should be encased in a span tag, however, in your case, it isn't.
What you could do, however, is select the div element encasing the text. Try this xpath :
(//div[#class='breadcrumb']/span)[3]/following-sibling::text()
You could try Abhijeet's Answer if you just want to get the text inside. As an added check, check if the string obtained from using getText() on root element contains the string obtained from using getText() on the child elements.

Finding all "A" tags with specific strings in the attribute href?

driver.FindElement(By.Name("zipcode")).Clear();
driver.FindElement(By.Name("zipcode")).SendKeys(zipcode);
driver.FindElement(By.Name("Go")).Click();
driver.FindElements(By.TagName("A"). //<---- ?????????
I have some Selenium API code that I started. I aim to get all the "A" tags with the string "alertsepy" and the sting "sevendwarves" in the attribute href and return all those elements into an array so I can do some further processing. I started the code but I am really not quite sure how to get all the way there yet. Does anyone know how to do this type of query with Selenium.
Kind Regards!
You should use css selector:
IList<IWebElement> elements = driver.findElements(By.cssSelector("a[href*=alertsepy],a[href*=sevendwarves]")
This query will return a nodes with href attribute that contains alertsepy or sevendwarves or both strings:
<a href="alertsepy.html" > </a>
<a href="sevendwarves.html" > </a>
<a href="http://sevendwarves.org/alertsepy.html" > </a>
Or you can use:
IList<IWebElement> elements = driver.findElements(By.cssSelector("a[href*=alertsepy][href*=sevendwarves]")
This query will return a nodes with href attribute that contains alertsepy and sevendwarves strings:
<a href="http://sevendwarves.org/alertsepy.html" > </a>
For a list of generally available css selectors refer to w3c css selectors. For the list of available in Selenium query types refer to Locating UI Elements.
List<WebElement> anchortaglist = driver.find Elements(By.Tag Name('a');

Using Selenium, xpath gets tags but text is empty

I'm using Selenium WebDriver to fetch a bunch of tags via XPATH. The xpath successfully finds the tags, but the "Text" attribute for the returned IWebElements is empty.
Here's the HTML:
<ul>
<li id="foo1">someValue</li>
<li id="foo2">someOtherValue</li>
</ul>
And the xpath:
//ul/li[startswith(#id, 'foo')]
Any ideas? The Xpath definitely grabs the right elements, but the Text element is empty.
Try this code to get the text of 2nd element - <li id="foo2">someOtherValue</li>
:
WebElement fooEle = driver.findElement(By.xpath("//descendant::ul/li[starts-with(#id,'foo')][2]"));
String fooEleText = fooEle.getText();
System.out.println("foo Element Text -" + fooEleText); //should print expected text "someOtherValue"
Note- if you want to get the 1st element text, then change the index value of the xpath
That is, //descendant::ul/li[starts-with(#id,'foo')][1]