xpath:
//*[#id='full-scorecard']/div[2]/div/table[1]/tbody/tr[3]/td[2]/child::text()
The above code only returns a webelement am not able to print the text.
WebElement txt= driver.findElement(By.xpath(
"//*[#id='full-scorecard']/div[2]/div/table[1]/tbody/tr[3]/td[2]/child::text()"));
System.out.println(txt);
HTML:
12.6
caught Hughes
73/4
this should work :
WebElement txt= driver.findElement(By.xpath("//*[#id='full-scorecard']/div[2]/div/table[1]/tbody/tr[3]/td[2]"));
System.out.println(txt.getText());
Selenium webdriver return all test under element object including child element.
So after reading you should remove integer values from text.
Thanks
Sadik
Related
I need to get the text of a td element with selenium. I have problem with extract text, I just receive null. I tried used list, getText() and so on. The HTML code is on the picture and element looks like you can see on the picture.
getDriver().findElement(By.xpath("//*[#id=\"standortTable\"]/tbody/tr/td[2]")).isDisplayed();
String test = getDriver().findElement(By.xpath("//*[#id=\"standortTable\"]/tbody/tr/td[2]")).getText();
System.out.println(test);
But I receive NULL, just "".
I guess you are missing a wait I.e. you trying to read the element text content before it is completely rendered.
Try this:
WebDriverWait wait = new WebDriverWait(getDriver(), 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//table[#id='standortTable']//td[#aria-colindex='2']")));
String test = getDriver().findElement(By.xpath("//table[#id='standortTable']//td[#aria-colindex='2']")).getText();
System.out.println(test);
The element is an Angular element, so to extract the text Teststandort1 you have to induce WebDriverWait for the visibilityOfElementLocated() and you can use either of the following Locator Strategies:
Using xpath and getText():
System.out.println(new WebDriverWait(getDriver(), 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//table[#id='standortTable']//tbody//tr[#class='ng-star-inserted']//td[#aria-colindex='2']"))).getText());
Using cssSelector and getAttribute("innerHTML"):
System.out.println(new WebDriverWait(getDriver(), 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("table#standortTable tbody tr.ng-star-inserted td[aria-colindex='2']"))).getAttribute("innerHTML"));
Code behind button:
<input class=”btn” id=”mypage:formid:relatedScenaiosListId:j_id27:j_id28″ name=”mypage:formid:relatedScenaiosListId:j_id27:j_id28″ onclick=”window.open(‘/apex/newscenario?Opportunity__c=006f00000072n8hAAA’,’_top’, 1);;A4J.AJAX.Submit(‘mypage:formid’,event,{‘similarityGroupingId’:’mypage:formid:relatedScenaiosListId:j_id27:j_id28′,’parameters’:{‘mypage:formid:relatedScenaiosListId:j_id27:j_id28′:’mypage:formid:relatedScenaiosListId:j_id27:j_id28′} } );return false;” value=”New” type=”button”>
I did a right click from the Inspect Element view and saw I could copy the Xpath and found it was :
//*[#id="mypage:formid:relatedScenaiosListId:j_id27:j_id28"]
Note the *.
I tired:
WebElement txtnew = driver.findElement(By.xpath(“//input[#id='mypage:formid:relatedScenaiosListId:j_id27:j_id28']“));
txtnew.click();
and
WebElement txtnew = driver.findElement(By.xpath(“//input[#id='mypage:formid:relatedScenaiosListId:j_id27:j_id28'][#value='New']“));
txtnew.click();
but neither worked.
I’m curious about the *, if that should be part of my Xpath statement?
If it's not necessary for you to use xpath, use searching by id, or cssSelectors to find your element. E.g.
//if you have only one element with class btn you can use this selector
//if element placed in parent (and this can identify element much more) add selector to
//parent before .btn
WebElement txtnew = driver.findElement(By.Css(".btn"));
//or
WebElement txtnew = driver.findElement(By.Css("input[value='New']"));
//or if id not generated automatically
WebElement txtnew = driver.findElement(By.Css("#mypage:formid:relatedScenaiosListId:j_id27:j_id28"));
//or using By.Id
WebElement txtnew = driver.findElement(By.Id("mypage:formid:relatedScenaiosListId:j_id27:j_id28"));
Any of them will work in specific situation. Choose one which more suitable for your sitution. Thanks.
Try this xpath:
input[#value='New'][#class='btn'][starts-with(#id, 'mypage:formid')]
The real issue, that I didn't realize, was that the control was in a different frame. Once I set the driver to look at the frame any number of xpath expressions worked.
` driver.manage().timeouts().implicitlyWait(25, TimeUnit.SECONDS);
driver.switchTo().frame("066i0000004bNpx");
WebElement txtNewbtn = driver.findElement(By.id("mypage:formid:relatedScenaiosListId:j_id27:j_id28"));
txtNewbtn.click();`
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 want to getText() using By.id or By.cssSelector.
I managed to solve my problem by doing getAttribute("value"), but I don't understand why getText() doesn't work like I expect it, and I might need it so all help is appreciated.
Here is the Java code:
WebDriverWait wait = new WebDriverWait(driver, 10);
Boolean elementIsPresent = wait.until(ExpectedConditions.textToBePresentInElementValue(By.cssSelector("#general_service_name"),"[reg] general_service_name")); // true
//WebElement general_service_name = driver.findElement(By.cssSelector("#general_service_name"));
WebElement general_service_name = driver.findElement(By.id("general_service_name"));
// Display check
Boolean isDisplayed;
if(general_service_name.isDisplayed()) isDisplayed = new Boolean(true); else isDisplayed = false; //true
String text_empty = general_service_name.getText(); //""
String text_with_value = driver.findElement(By.id("general_service_name")).getAttribute("value"); //"[reg] general_service_name"
And HTML:
<input id="general_service_name" type="text" value="[reg] title" name="general_service_name" style="float:left;"/>
http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/WebElement.html#getText()
getText() delivers the innerText of a WebElement.
Your input field does not have any inner Text. The text is found inside your value-attribute, hence accessing it via getAttribute("value") is the correct way to do it.
Java
ele.getAttribute("innerHTML");
This could get the text already in the background and not displayed on the page yet.
Simple answer - it's designed this way. getText() parses the content of the tag (i.e. its innerText), which is obviously empty for inputs.
You may use this if you want to search for a given text on a WebElement. Pass it directly or through a string:
String textoToSearch = "Text inside Webelement";
driver.findElement(By.id("someID).getText().contains("textToSearch");
getText()
getText() returns the visible text of this element.
java.lang.String getText()
Get the visible (i.e. not hidden by CSS) text of this element, including sub-elements.
Returns:
The visible text of this element.
As per the HTML of the element:
<input id="general_service_name" type="text" value="[reg] title" name="general_service_name" style="float:left;"/>
The WebElement doesn't have a visible text but the value attribute have the value set as [reg] title.
So to extract the value of the value attribute i.e. [reg] title you can use either of the following Locator Strategies:
Using cssSelector:
System.out.println(wd.findElement(By.cssSelector("input#general_service_name[name='general_service_name']")).getAttribute("value"));
Using xpath:
System.out.println(wd.findElement(By.xpath("//input[#id='general_service_name' and #name='general_service_name']")).getAttribute("value"));
Ideally, you need 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(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input#general_service_name[name='general_service_name']"))).getAttribute("value"));
Using xpath and getAttribute("innerHTML"):
System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[#id='general_service_name' and #name='general_service_name']"))).getAttribute("value"));
This question received great answers in jquery and I was wondering if someone could give an example of this in Java please?
I'm doing driver.findElement(By.className("current time")).click(); The space is the issue, and I see the explanation at the link, but I'm not sure how to handle it in java, and don't have access to change the class name.
Pasting example of what i get in the firefox inspect id: Example with cssSelector below did not work, but i may be missing something.
<span>
<a class="current time" href="http://someurl/" onclick="s_objectID="http://someur/">url</a>
</span>
Instead of class name you can use a css selector. You don't mention the tagname for the class 'current time'. I am assuming it to be input, so your css selector work be,
WebElement element = driver.findElement(By.cssSelector("input[class='current time']"));
element.click();
Edit#1 Based on html provided,
Looking at the html in your comment, it seems you have quite a few options to find the webElement. Here are your options,
WebElement element = driver.findElement(By.cssSelector("a[class='current time']"));
element.click();
or this should work too,
WebElement element = driver.findElement(By.cssSelector("a.current.time"));
element.click();
You can also use linkText since the element is link. From the html you provided, the link text is 'url'
WebElement element = driver.findElement(By.linkText("url"));
element.click();
You can also use By.partialLinkText("partial link text here");
You can also use xpath as:
WebElement element = driver.findElement(By.xpath("//a[#class='current time']"));
element.click();
OR,
WebElement element = driver.findElement(By.xpath("//a[text() = 'url']"));
element.click();
For a less fragile test, another option is to use an XPATH which doesn't depend of the order of classes, like:
WebElement element = driver.findElement(By.xpath("//a[contains(#class, 'current') and contains(#class, 'time')]"));
Whenever you found some space in the class name you need to switch to cssSelector Locator.
Convert a class name to cssSelector if it is having a space as below.
In your case it would be:
WebElement element = driver.findElement(By.cssSelector(".current.time"));
element.click();
PS: add . [dot] in start of class name and replace the space with . [dot] to convert class name to cssSelector.