Here is the sample webpage code
<div class="size1of2 fllt">
<div id="iad-service" class="tmargin2 rite fllt service-check"></div>
<div class="fk-font-13 fk-font-regular">hi</div>
</div>
I want to find the "class" element using Selenium WebDriver.
Here is the code I tried.
String abc = driver.findElement(By.xpath("//div[contains(#id,'iad-service')]/#class")).getText();
System.out.println(abc);
When I tried this code(//div[contains(#id,'iad-service')]/#class) in the XPath Checker Addon, I am getting this output.
tmargin2 rite fllt service-check
But using WebDriver, I am getting an error. I want the output to be the content of the class which is.
tmargin2 rite fllt service-check
Where am I doing wrong?
You need to fetch the div element, then retrive the class attribute value:
String abc = driver.findElement
(By.xpath("//div[contains(#id,'iad-service')]")).getAttribute("class");
You can use this:
WebElement id=wd.findElement(By.id("iad-service"));
String className=id.getAttribute("class");
Related
I want to get text included in a span tag.
Here's HTML
<div class="position">
<h1>
<span class="select-text">Some Text</span>
</h1>
</div>
And I've tried this
wd.findElement(By.xpath("//div[#class="position"]//h1//span")).getText();
But I get this error all the time and IDK what I'm doing wrong
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//div[#class="position"]//h1//span"}
Additional problem: after getting a text from this page I need to make sure if this text is not visible on another page. If you have any idea how can I realize it I'll be grateful.
I believe you will find that your xpath selector for div with class 'position' should be
div[contains(#class, 'position')]
To print the text Some Text from the <span> you can use either of the following Locator Strategies:
Using cssSelector and getAttribute("innerHTML"):
System.out.println(wd.findElement(By.cssSelector("div.position>h1>span.select-text")).getAttribute("innerHTML"));
Using xpath and getText():
System.out.println(wd.findElement(By.xpath("//div[#class='position']/h1/span[#class='select-text']")).getText());
Ideally, to extract the text Some Text as the element is a dynamic element, you have to induce WebDriverWait for the visibilityOfElementLocated() and you can use either of the following Locator Strategies:
Using cssSelector and getText():
System.out.println(new WebDriverWait(wd, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div.position>h1>span.select-text"))).getText());
Using xpath and getAttribute("innerHTML"):
System.out.println(new WebDriverWait(wd, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[#class='position']/h1/span[#class='select-text']"))).getAttribute("innerHTML"));
Try this xpath:
wd.findElement(By.xpath("//div[contains(#class='position')]/h1/span[contains(text(), 'Some Text') ]")).getText () ;
If you need to check, if this text exists in another page or not, you need to write the generic xpath, which will capture the results.
Compare your current string with another text using if loop.
I am using selenium with java and want to fetch the value "3". I guess I have to use xpath but I am not sure what would be the syntax for that? the html code is given below:
<div class="p-panel halign-center">
<div>
<span class="p-text p-f-sz-xl p-t-secondary50 p-f-w-b p-t-wr-fw" data-tag="Transcript-Summary-No-Due-Date-Count">3</span>
</div>
</div>
You can fetch value 3 by following code
String value = driver.findElement(by.xpath("//span[#data-tag='Transcript-Summary-No-Due-Date-Count']").getText();
OR
String value = driver.findElement(by.xpath("//span[#class='p-text p-f-sz-xl p-t-secondary50 p-f-w-b p-t-wr-fw']").getText();
Also we can improve the xpath if more HTML code is available.
Happy coding~
This is for getting by class:
.//span[#class='p-text p-f-sz-xl p-t-secondary50 p-f-w-b p-t-wr-fw']
This is for getting by text:
.//span[text()='3']
and this is both
.//span[#class='p-text p-f-sz-xl p-t-secondary50 p-f-w-b p-t-wr-fw' and text()='3']
I'm trying to get the number of followers on twitter. I successfully managed to get number of followers like so:
String followers = driver.findElement(By.xpath("//div[#class='ProfileCanopy-navBar']//li[#class='ProfileNav-item ProfileNav-item--followers']//span[#class='ProfileNav-value']")).getText();
The problem is that the answer is not the exact number, "4.41M".
The HTML:
<a class="ProfileNav-stat ProfileNav-stat--link u-borderUserColor u-textCenter js-tooltip js-nav" data-nav="followers" tabindex="0" data-original-title="4,406,048 Followers">
<span class="ProfileNav-label">Followers</span>
<span class="ProfileNav-value" data-is-compact="true">4.41M</span>
</a>
I'm trying to get the number "4,406,048" (at the end of attribute a). I looked online for about an hour and didn't find a proper solution. I'm using Selenium with Java and Chrome.
This was a strange one. I wrote code that should have pulled the number but it kept returning null also. I finally figured out what was going on when I pulled the element and then wrote out the outerHTML. The element was being changed during page load.
WebDriver driver = new FirefoxDriver();
driver.get("https://twitter.com/blakeshelton");
WebDriverWait wait = new WebDriverWait(driver, 5);
WebElement e = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("a[data-nav='followers']")));
System.out.println(e.getAttribute("outerHTML"));
System.out.println(e.getAttribute("title"));
If you run this code you will get
<a class="ProfileNav-stat ProfileNav-stat--link u-borderUserColor u-textCenter js-tooltip js-openSignupDialog js-nonNavigable u-textUserColor" title="14,189,678 Followers" data-nav="followers" href="/blakeshelton/followers">
<span class="ProfileNav-label">Followers</span>
<span class="ProfileNav-value" data-is-compact="true">14.2M</span>
</a>
14,189,678 Followers
You will notice in the A tag in the outerHTML that title contains the number of followers. That's why I'm using it instead of data-original-title. Anyway, this code has been tested and it works.
Since you reference Selenium, this is where you use getAttribute() to return the value of a given attribute. In this case we want the value of 'data-original-title'.
driver.findElement(By.cssSelector("[data-nav='followers']")).getAttribute("data-original-title");
Then, since this returns more data than you desire (x followers), you strip out the non-numerics with some Java:
replaceAll("[$A-Za-z , ]", "");
So put together it looks something like this:
String followers = driver.findElement(By.cssSelector("[data-nav='followers']"))
.getAttribute("data-original-title").replaceAll("[$A-Za-z , ]", "");
You were using getText() which returns the inner text of an element. GetAttribute() returns value of an attribute, which in this case is data-original-title.
Here's the code I used to confirm it works on the HTML you provided.
public static void main(String[] args)
{
ChromeDriver driver = new ChromeDriver();
driver.get("file:///C:/Users/myId/Downloads/stack.html");
String followers = driver.findElement(By.cssSelector("[data-nav='followers']"))
.getAttribute("data-original-title").replaceAll("[$A-Za-z , ]", "");
System.out.println(followers);
}
you can test that in chrome using the inspection tools and then testing your xpath.
You should be doing something like that:
$x("//a[#data-nav='followers']/#data-original-title")
This got me
[data-original-title="1,880,556 Followers"]
From there just evaluate in Java.
I want to find a p tag with class = "big-number". Here is the code I wrote:
WebElement myDynamicElement = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.className("big-number")));
System.out.println(driver.getTitle());
System.out.println(myDynamicElement);
Here is my output:
[[FirefoxDriver: firefox on MAC (fed46ad4-9ca9-9344-a57a-1d336db3927c)] -> class name: big-number]
I cannot identify the error, it is giving me an output but its makes no sense to me.
Any tips on how I can at least identify my error?
I am certainly sure the element is present, here is the HTML code:
<div id="users-online-container" style="">
<img class="big-number-icon" src="images/usersOnline.png">
<p class="big-number">228</p>
<p class="caption">Users Online</p>
</div>
<div id="users-online-loading"></div>
TimeOutException occurs because driver cannot find element in specific time. Problem in selector i think. If you sure that element always visible, and exists on the page so try next code:
//Select first paragraph in div
driver.FindElement(By.CssSelector("#users-online-container .big-number"));
//if you have several p with same classes you could access any of them using index. e.g.
driver.findElements(By.CssSelector(".big-number"))[index];
Selectors can be #users-online-container .big-number or .big-number. Both will work.
Try below code..
WebElement myDynamicElement = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.className("big-number")));
// It will print the text of the Element:
system.out.println(myDynamicElement.getText());
Also you try to locate the element with the help of XPATH and make sure your locator is uniquely identify the element. Also check that IsDisplayed() and IsEnabled() returns True.
In your code you are printing the WebElement that will print the Hashcode.
In order to get the text of the Element, you'll have to use getText() method.
Hope it will help!
I have a span element like this
<span id="yui-gen56" class="ir i-doc" style="background-color: transparent;"></span>
Now I want to retrieve the class attribute value so that it should return me ir i-doc for verification. Any idea how can I get this attribute value for further verification.
Thanks in advance
You can try something like below where driver is object of WebDriver -
WebElement webElement = driver.findElement(By.id("yui-gen56"));
webElement.getAttribute("class");
Try this...
WebElement cssValue = driver.findelement(By.cssSelector("[id='yui-gen56'][class='ir i-doc']"));
String Value = divElement.getText();
System.out.println(Value);
It helped me in finding value of css class..