How to click a link from list selenium2 - java

I'm using Selenium 2 and I want to click an 'invite' link for Name3. How can I do that?
here is the html code:
<ul>
<li>
<label for="511565484">
<img src="pic1">Name1</label>
<a class="button_green sendInvite" href="javascript:;" title="Invite">Invite</a>
</li>
<li>
<label for="535963597">
<img src="pic2">Name2</label>
<a class="button_green sendInvite" href="javascript:;" title="Invite">Invite</a>
</li>
<li>
<label for="561708219">
<img src="pic3">Name3</label>
<a class="button_green sendInvite" href="javascript:;" title="Invite">Invite</a>
</li>
</ul>

Seems likely it can only be done with XPath:
//label[text()='Name3']/following-sibling::a

element2 = driver.findElement(By.xpath("//img[()text='Name3']/a"));
element2.click();

If XPATH isn't the most usable thing for you, you can always do something like this (Ruby implementation of Webdriver... but it's all the same):
invite_links = driver.find_elements(:class_name, "sendInvite")
invite_links now contains an array of all matches, so your next step is pretty easy:
invite_links[2].click()
Or the way I'd do it:
driver.find_elements(:class_name, "sendInvite")[2].click
This is a little easier for me to read than XPATH, because I don't use it that often.

Related

How to find element using selenium

I have a list of elements:
<ul>
<li></li>
<li></li>
<li style="display: none"></li>
<li style="display: none"></li>
<li style="display: none"></li>
<li></li>
</ul>
I want to take only elements without this style="display: none".
Any suggestions how to distinguish between those with style="display: none" and those without?
List<WebElement> lis = webDriver.findElement(By.cssSelector("ul li:not([style*='display: none'])"));
List without display : none element...
This allows you to get all <li> elements that don't have style="display: none" in their HTML code :
driver.findElement(By.cssSelector('li:not([style="display: none"])'));
How about doing ,
String s = webelement.getAttribute("outerHTML");
Now use the String s to parse the style attribute.
You can use cssSelector with :not
driver.findElements(By.cssSelector("li:not([style='display: none']"));
This will get all <li> tags that doesn't have style='display: none'

Please help in finding the XPath:

I've the below snippet, I need to find the Xpath to click on "Option1"
The Problem here is below two divisions are dynamically displayed based on the browser size or Screen Resolution
<div class="wrap">
<aside id="side-anchor-links">
<ul class="menu">
<li><a class="anchor-active">Option1</a></li>
<li"><a class="">Option2</a></li>
</ul>
</aside>
<main class="content">
<div id="wrapper">
<div id="scroller">
<div id="top-anchor-links" class="scroll-me">
<ul class="menu">
<li><a class="">Option1</a></li>
<li><a class="">Option2</a></li>
</ul>
</div>
</div>
</div>
</main>
</div>
Thanks is Advance.
My Trails:
xpath = //aside[#id="side-anchor-links"]/ul/li[text()="Option1"] //This option will work only when the options are displayed in the side view
xpath = //li[text()="Option1"] //This option will retrieve two occurances
Note: Always class="anchor-active" is associated with option1 in Side View until we click on any option from any division
Try with the below xpath :
//div[#class='wrap']/aside[#id="side-anchor-links"]/ul/li[1]
So, I believe you need to click at one element, and if it is not present - on another.
And if plain //li[text()='Option1'] does not suite you well, try this:
//*[#id='side-anchor-links' or #id='top-anchor-links']//li/a
Better customize your //li/a part, however

How to print the all li items text of dropdown using Selenium WebDriver?

How I print the all li items text using selenium webdriver.
Here I want to some type of single line code that print all li items text like
Jackets,
protective etc. May by using for loop
<div class="dropdown change_right">
<ul>
<li>
<a class="" title="Jackets" href="/collections/jackets">Jackets</a>
</li>
<li>
<a title="Jackets protective" href="/collections/jackets/jackets-protective" class="sub-link"> protective</a>
</li>
<li>
<a title="casual leather" href="/collections/jackets/casual-leather" class="sub-link">casual leather</a>
</li>
<li>
<a class="" title="T-shirts & shirts " href="/collections/t-shirts-shirts">T-shirts & shirts </a>
</li>
<li>
<a title="shirts" href="/collections/t-shirts-shirts/shirts" class="sub-link">shirts</a>
</li>
<li>
<a title="crew-neck" href="/collections/t-shirts-shirts/crew-neck" class="sub-link">crew-neck</a>
</li>
<li>
<a title="polo tshirts" href="/collections/t-shirts-shirts/polo-tshirts" class="sub-link">polo tshirts</a>
</li>
<li>
<a class="" title="Trousers" href="/collections/trousers">Trousers</a>
</li>
<li>
<a title="Trousers Protective" href="/collections/trousers/trousers-protective" class="sub-link"> Protective</a>
</li>
</ul>
Can be done with a simple findElements(). With the help of xpath to grab the collection of elements and a simple iterate through the collection and print the value with the help of getText()which returns the string value of the element.
List<WebElement> allText = driver.findElements(By.xpath("//*[contains(#class,'dropdown')]//a"));
for ( WebElement element: allText) {
System.out.println(element.getText());
}
Note: untested code. You may need to make sure click and open the list happens before executing this code and some wait might also be necessary as well

JSoup scrape HTML document by attribute value

I want to make a dynamic website and need some pics off the internet. I decided to scrape them off flickr and include the owners on my website but am running into problems scraping. I'll post part of the HTML below but if you want to check the source code yourself, here's the website. https://www.flickr.com/explore
HTML:
<div class="thumb ">
<span class="photo_container pc_ju">
<a data-track="photo-click" href="/photos/sheilarogers13/15586482942/in/explore-2014-10-20" title="Lake District" class="rapidnofollow photo-click"><img id="photo_img_15586482942" src="https://c2.staticflickr.com/4/3945/15586482942_6a7154363f_z.jpg"width="508" height="339" alt="Lake District" class="pc_img " border="0"><div class="play"></div></a>
</span>
<div class="meta">
<div class="title"><a data-track="photo-click" href="/photos/sheilarogers13/15586482942/in/explore-2014-10-20" title="Lake District" class="title">Lake District</a></div>
<div class="attribution-block">
<span class="attribution">
<span>by </span>
******<a data-track="owner" href="/photos/sheilarogers13" title="sheilarogers22" class="owner">sheilarogers22</a>******
</span>
</div>
<span class="inline-icons">
<a data-track="favorite" href="#" class="rapidnofollow fave-star-inline canfave" title="Add this photo to your favorites?"><img width="12" height="12" alt="[★]" src="https://s.yimg.com/pw/images/spaceball.gif" class="img"><span class="fave-count count">99+</span></a>
<a title="Comments" href="#" class="rapidnofollow comments-icon comments-inline-btn">
<img width="12" height="12" alt="Comments" src="https://s.yimg.com/pw/images/spaceball.gif">
<span class="comment-count count">57</span>
</a>
<img width="12" height="12" alt="" src="https://s.yimg.com/pw/images/spaceball.gif">
</span>
</div>
</div>
I want the line where I put asterisks, in order to be able to give credit to the authors of the pictures.
My code:
Elements pgElem = doc.select("div.thumb").select("div.meta").select("[data-track]");
The above code however gives me all 4 data tracks in my div.meta though, and I only want the one that =owner.
I checked the JSoup documentation and it says that attributes with values are found using [attr=value], but I can't seem to get it to work. I've tried:
.select("[data-track=owner]")
.select("[data-track='owner']")
but neither work. Thoughts?
Elements pgElem = doc.select("div.thumb").select("div.meta").select("[data-track]");
Elements ownerElements = new Elements();
for(Element element:pgElem){
if(!element.getElementsByAttributeValueContaining("data-track","owner").isEmpty()){
ownerElements.add(element);
}
}
actually, I just gave it another spin and this works for me:
doc.select("div.thumb").select("div.meta").select("[data-track=owner]")

How to retrieve atomic values inside tags in HTMLUnit

I am new to HtmlUnit and I don't know how to get the text inside the [...]
A part of my html file:
<ul ......somethin....>
<li data-role="list-divider" role="heading" style="font-size:16px;" class="ui-bar-f">
INFORMATION_LINE_1
</li>
<li data-theme="d" class="ui-li ui-btn-icon-right ui-btn-up-d ui-odd-match-column ">
<div class="ui-btn-inner ui-li">
<div class="">
<div class="ui-btn-text">
<a href="/x/cxntay/13113/ndzvsssl/g1" class=" ui-link-inherit ui-link-hover">
<h3 class="ui-li-heading">
<span class="xheader">INFORMATION_LINE_2</span>
<span class="label live">INFORMATION_LINE_3</span>
</h3>
<div class="ui-live-scores">
<span class="team1-scores">
<span class="ui-team-name">INFORMATION_LINE_4</span>
<span style="font-weight:bold">INFORMATION_LINE_5</span>
</span>
<span>INFORMATION_LINE_6</span>
</div>
</a>
</div>
</div>
</div>
</li>
</ul>
Now, I want to retrieve "INFORMATION_LINE_X"(1,2...6) in between these tags..
This is what I tried:
List<HtmlUnorderedList> ls = (List<HtmlUnorderedList>) page.getByXPath("/ul");
List<DomNode> dls = ls.get(0).getChildNodes();
System.out.println(dls.get(0).getFirstByXPath("//li[#data-role='list-divider']/text()");
I just tried to get INFORMATION_LINE_1
But it printed null. I need to get all the INFORMATION_LINES.
It is better to use just XPath rather than mixing it with HTMLUnit methods. Something like this should work to get you the first information line:
HtmlElement e = page.getFirstByXPath("//li[#data-role='list-divider']");
System.out.println(e.asText());
In order to fetch the other information lines you should follow the same approach but changing the XPath string.
Bear in mind you should always debug the page by taking a look at the code by printing the output of page.asXml(). If you use a real browser you are not actually seeing exactly the same as HTMLUnit is seeing. You can stumble with differences particularly if the page executes JavaScript.

Categories