I am having trouble with clicking at an element of a menu which is written like this:
<div class="menu">
<ul class="tabs ctrlTabsProfile">
<li class="active" data-tab="tabDetail">User Details</li>
<li data-tab="tabEmail">Email</li>
<li data-tab="tabPass">Change password</li>
<li data-tab="tabAdress">Account Details</li>
</ul>
</div>
I have tried these:
driver.findElement(By.linkText("Account Details")).click();
driver.findElement(By.cssSelector("li[data-tab=tabAdress")).click();
driver.findElement(By.xpath("li[data-tab='tabAdress']")).click();
also tried listing the elements but got null only :
for(WebElement el : driver.findElements(By.cssSelector(".tabs.ctrlTabsProfile"))) {
try {
assertTrue(driver.findElement(By.cssSelector("BODY")).getText().matches("^[\\s\\S]*Account Details[\\s\\S]*$"));
} catch (Error e) {
System.out.println("Not found: \"Account Details\".");
}
String s = el.getAttribute("data-tab");
System.out.println(s);
if(s.equals("tabAdress")) {
driver.findElement(By.xpath("li[data-tab='tabAdress']")).click();
}
}
Solutions? Sugestions? Errors?
Well, for one, your xpath selector is incorrect.
driver.findElement(By.xpath("li[data-tab='tabAdress']")).click();
should be:
driver.findElement(By.xpath("//li[#data-tab='tabAdress']")).click();
edit:
And your css selector is incorrect as well.
driver.findElement(By.cssSelector("li[data-tab=tabAdress")).click();
should be:
driver.findElement(By.cssSelector("li[data-tab='tabAdress']")).click();
edit #2:
and:
driver.findElement(By.linkText("Account Details")).click();
will only work if the element is a link, which in this case it is not.
Aholt is right, driver.findElements(By.cssSelector(".tabs.ctrlTabsProfile")) will return only ul elements. To access all <li>, you could try:
driver.findElements(By.cssSelector("ul.tabs.ctrlTabsProfile li.active"))
Related
i am trying to display two "text text-pass" from html in chrome browser to my print console, apparently, it did not work, any advise please?
my browser html code
<a href="/abc/123" class="active">
<div class="sidebar-text">
<span class="text text-pass"> </span> </a>
<a href="/abc/1234" class="active">
<div class="sidebar-text">
<span class="text text-pass"> </span> </a>
My code
String 123= driver.findElement(By.xpath("//*[#id="js-app"]/div/div/div[2]/div[1]/div/div/ul/li[5]/a")).getText();
System.out.println(123);
String 1234= driver.findElement(By.xpath("//*[#id="js-app"]/div/div/div[2]/div[1]/div/div/ul/li[5]/a")).getText();
System.out.println(1234);
You can use .findElements to get multiple elements with the same pattern, it will return a list collection.
UPDATE
Refers to your comment, you need put the string into a list again and check with the Collection.contains() method:
List<String> results = new ArrayList<>();
List<WebElement> elements = driver.findElements(By.xpath("//div[#class='sidebar-text']//span"));
for(WebElement element: elements) {
String attr = element.getAttribute("class");
results.add(attr);
System.out.println(attr);
}
if(results.contains("text text-fail")) {
System.out.println("this is list contains 'text text-fail'");
}
Try this Code :
String pass = driver.findElement(By.xpath("//*[#class='sidebar-text']/span")).getAttribute("class");
System.out.println(pass);
I am automating extracting product variations on Amazon and I have the following HTML markup:
<ul
class="a-nostyle a-button-list a-horizontal a-spacing-top-micro swatches swatchesSquare imageSwatches">
<!-- Please note that in className never append a class with prefix as 'swatch'. It would break something in the twister JS -->
<li id="color_name_0" class="swatchSelect" data-dp-url="" title="Click to select White">
<span class="a-list-item">
<div class="tooltip">
<span class="a-declarative" data-swatchthumb-action="{"dimIndex":1,"dimValueIndex":0}" data-action="swatchthumb-action">
<span id="a-autoid-11" class="a-button a-button-thumbnail a-button-toggle">
<span class="a-button-inner">
<button id="a-autoid-11-announce" class="a-button-text" type="button">
<span class="xoverlay" />
<div class="">
<div class="">
<img style="height:36px; width:36px" alt="White"
src="http://ecx.images-amazon.com/images/I/41IrdkWxWOL._SS36_.jpg"/>
</div>
<div class="" style=" " />
</div>
</button>
</span>
</span>
</span>
</div>
</span>
</li>
</ul>
I'm using the following XPath to extract the XPath of all colors.
.//*[#id='variation_color_name']/ul/li/span/div/span/span/span/button
Now I want to extract the alt attribute of each item but when I try using getAttribute("alt") it does not return anything. In this case the alt text would be "White". The product I am viewing is: http://www.amazon.com/dp/B00J46VVKE . I'm using Java.
When you have an id attribute there is no need to go to xpath's i believe, unless you have many button's with same id. However here's how you can get the attribute of the img element -
WebElement btn = driver.findElement(By.id("a-autoid-11-announce"));
String imgColor = btn.findElement(By.tagName("img")).getAttribute("alt");
Hope this helps.
You can use this code:
public class Stackoverflow extends Init {
#Test
public void testToGetAltAttribute() throws InterruptedException {
System.out.println("Get Attribute....");
// this element has alt attribute hence that will be displayed.
assertAndVerifyElement(By.cssSelector("#landingImage"));
System.out.println("\n#landingImage\n=====================");
System.out.println(getAttributeOfGivenElement(By.cssSelector("#landingImage"), "alt"));
// this element do not has alt attribute hence that will not be
// displayed.
// it will display msg "element do not have altattribute"
assertAndVerifyElement(By.id("productTitle"));
System.out.println("\n#productTitle\n=====================");
System.out.println(getAttributeOfGivenElement(By.id("productTitle"), "alt"));
}
public String getAttributeOfGivenElement(By element, String attributeName) {
WebElement webElement = getWebDriver().findElement(element);
if (webElement.getAttribute(attributeName) != null) {
return webElement.getAttribute("alt");
} else {
return "element do not have " + attributeName + "attribute";
}
}
public void assertAndVerifyElement(By element) throws InterruptedException {
boolean isPresent = false;
for (int i = 0; i < 5; i++) {
try {
if (getWebDriver().findElement(element) != null) {
isPresent = true;
break;
}
} catch (Exception e) {
// System.out.println(e.getLocalizedMessage());
Thread.sleep(1000);
}
}
Assert.assertTrue(isPresent, "\"" + element + "\" is not present.");
}
}
If you want all the colors, you will want to grab the IMG elements that contain the ALT attribute. Your XPath ends at a BUTTON. Try the code below.
List<WebElement> colors = driver.findElements(By.cssSelector("ul.imageSwatches img"));
for (WebElement color : colors)
{
System.out.println(color.getAttribute("alt"));
}
The CSS selector is read find a UL tag that has the class imageSwatches then find all descendant IMG tags. You loop through that collection of IMG tags and output the ALT text.
I have HTML code like :
<div class="ex1">
<div class="ex2">
<span>test1</span>
<span class="ex3">test2</span>
</div>
<div class="ex2">
<span>test3</span>
<span class="ex3">test2</span>
</div>
</div>
I'm using Selenium Webdriver.
And I need to create Java code which could:
If <span>test3 then select a <span class="ex3"> which located inside the same div class="ex2"
But since I have div's and spans with the same className inside one main I can't differ this spans..
Could you help me please with this issue?
So,something like this:
If <span>test3 then <span class=ex3>test2.
or
If <span>test1 then <span class=ex3>test2.
Thanks
1- Use this xpath to get to <span>test1 then <span class=ex3>test2:
//span[.='test1']/following-sibling::span[.='test2']
And, use in code like this:
WebElement ele = driver.findElement(By.xpath("//span[.='test1']/following-sibling::span[.='test2']"));
2-And, Use this xpath to get to <span>test3 then <span class=ex3>test2:
//span[.='test3']/following-sibling::span[.='test2']
Use in code like this:
WebElement ele = driver.findElement(By.xpath("//span[.='test3']/following-sibling::span[.='test2']"));
I have implemented the If statement as per your question in practice you can modify it as per your requirement.
WebElement test1_class1 =wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#class='ex1']//div[1]//span[1]")));
String test1= test1_class1.getText();
WebElement test2_class1 =wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#class='ex1']//div[1]//span[2]")));
String test2_1 =test2_class1.getText();
WebElement test3_class2 =wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#class='ex1']//div[2]//span[1]")));
String test3 =test3_class2.getText();
WebElement test2_class2 =wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#class='ex1']//div[2]//span[2]")));
String test2_2 =test3_class2.getText();
try
{
if(test1.equals("test1"))
{
System.out.println(test2_1);
}
if(test3.equals("test3"))
else
{
System.out.println(test2_2);
}
}
catch(Throwable e)
{
System.out.println("Exception in program"+e);
}
I'm relatively new to using jsoup, and I can't seem to find the correct query to parse out the value I'm looking for. The HTML is as follows.
<img src='http://rootzwiki.com/public/style_images/ginger/t_unread.png' alt='New Replies' /><br />
</a>
</td>
<td class='col_f_content '>
<h4><a id="tid-link-12251" href="http://rootzwiki.com/topic/12251-romlte-rootzboat-403-v61/" title='View topic, started 17 December 2011 - 09:32 AM' class='topic_title'>[ROM][LTE] RootzBoat 4.0.3 V6.1</a></h4>
<br />
<span class='desc lighter blend_links'>
Started by <a hovercard-ref="member" hovercard-id="5" class="_hovertrigger url fn " href='http://rootzwiki.com/user/5-birdman/'>birdman</a>, 17 Dec 2011
</span>
<ul class='mini_pagination'>
<li><a href="http://rootzwiki.com/topic/12251-romlte-rootzboat-403-v61/" title='Go to page 1'>1</a></li>
<li><a href="http://rootzwiki.com/topic/12251-romlte-rootzboat-403-v61/page__st__10" title='Go to page 2'>2</a></li>
<li><a href="http://rootzwiki.com/topic/12251-romlte-rootzboat-403-v61/page__st__20" title='Go to page 3'>3</a></li>
<li><a href="http://rootzwiki.com/topic/12251-romlte-rootzboat-403-v61/page__st__1990" title='Go to page 200'>200 →</a></li>
</ul>
</td>
<td class='col_f_preview __topic_preview'>
<a href='http://rootzwiki.com/topic/12251-romlte-rootzboat-403-v61/' class='expander closed' title='Preview this topic'> </a>
</td>
<td class='col_f_views desc blend_links'>
<ul>
<li>
<span class='ipsBadge ipsBadge_orange'>Hot</span>
1,999 replies
</li>
<li class='views desc'>180,213 views</li>
</ul>
</td>
<td class='col_f_post'>
<a href='http://rootzwiki.com/user/49940-jakeday/' class='ipsUserPhotoLink left'>
<img src='http://rootzwiki.com/uploads/profile/photo-thumb-49940.jpg' class='ipsUserPhoto ipsUserPhoto_mini' />
</a>
<ul class='last_post ipsType_small'>
<li><a hovercard-ref="member" hovercard-id="49940" class="_hovertrigger url fn " href='http://rootzwiki.com/user/49940-jakeday/'>jakeday</a></li>
<li>
<a href='http://rootzwiki.com/topic/12251-romlte-rootzboat-403-v61/page__view__getlastpost' title='Go to last post'>Today, 04:20 AM</a>
</li>
</ul>
</td>
I need to parse out birdman from there. I know that once I've defined the element, I can get "birdman" out with author.text();, but I cant figure out how to define the author element. I thought perhaps the following block of code would work, but as I mentioned, I'm pretty new to jsoup and html and it obviously didnt work. Theres nothing wrong with the connection, and jsoup is working for the other values I parsed out.
TitleResults titleArray = new TitleResults();
Document doc = null;
try {
doc = Jsoup.connect(Constants.FORUM).get();
} catch (IOException e) {
e.printStackTrace();
}
Elements threads = doc.select(".topic_title");
for (Element thread : threads) {
titleArray = new TitleResults();
//Thread title
threadTitle = thread.text();
titleArray.setItemName(threadTitle);
//Thread link
String threadStr = thread.attr("abs:href");
String endTag = "/page__view__getnewpost"; //trim link
threadStr = new String(threadStr.replace(endTag, ""));
threadArray.add(threadStr);
titleArray.setAuthorDate("Author/Date");
results.add(titleArray);
}
Elements authors = doc.select("a[hovercard-ref]");
for (Element author : authors) {
if (author.attr("abs:href").contains("/user/")){
Log.d("POC", "SUCCESS " + author.attr("abs:href"));
} else {
Log.d("POC", "FAILURE " + author.text());
}
}
}
I think you're thinking too hard ;)
To get the birdman portion of the link, just use the following:
Elements authors = doc.select("a");
for (Element author : authors) {
Log.d("POC", author.text());
}
The "a" retrieves all links. After that you can just use the .text() like you said to retrieve the value.
Selvin answered it in the comments. I wasnt getting the source correctly and it was causing errors.
http://pastebin.com/xfUQkGw0
I have a HTML with the following format
<article class="cik" id="100">
<a class="ci" href="/abc/1001/STUFF">
<img alt="Micky Mouse" src="/images/1001.jpg" />
<span class="mick vtEnabled"></span>
</a>
<div>
Micky Mouse
<span class="FP">$88.00</span> <span class="SP">$49.90</span>
</div>
</article>
In the above code the tag inside article has a span class="mick vtEnabled" with no lable. I want to check if this span tag with the class name specified is present within the article tag. How do i do that? I tried select("> a[href] > span.mick vtEnabled") and checked the size..it remains 0 for all the article tags irrespective if its set or not. any inputs?
Element span = doc.select("article.cik > a.ci > span.mick.vtEnabled").first();
if(span != null){
System.out.println("Exist!!");
}
else {
System.out.println("No Span :(");
}
This
Elements divs = doc.select("article > a[href] > span[class=mick vtEnabled]");
selects the div with the two classes.