This is a continuation from my other post
Using JSoup to get data-code value of a table
I am trying to get the text inside the <span> tags on the table using the cssSelector() method in Selenium webdriver
<table class ="team-list">
<tr data-code="1">
<td>
<span>
Get This Text
</span>
</td>
</tr>
</table>
I have tried the following code, but this will print out the text in all cells for each row, but i only need to get the one inside the <span> tags
WebDriver driver = new FirefoxDriver();
driver.get("http://www.example.com");
List<WebElement> elements = driver.findElements(By.cssSelector("table.team-list td"));
for(WebElement element: elements)
{
System.out.println(element.getText());
}
if you know the text you are looking for you can the following:
WebDriver driver = new FirefoxDriver();
driver.get("http://www.example.com");
List<WebElement> elements = driver.findElements(By.cssSelector("table.team-list td"));
for(WebElement element: elements)
{
if(element.getText().equals("Get This Text"))
System.out.println(element.getText());
}
That's might be o(n) but if you are not care about performance that could solve your problem.
I think you should change your selector to this:
By.cssSelector("table.team-list span")
Try something like this.
WebDriver driver = new FirefoxDriver();
driver.get("http://www.example.com");
List<WebElement> elements = driver.findElements(By.cssSelector("table.team-list td"));
for(WebElement element: elements)
{
try{
System.out.println(element.findElement(By.tagName("span")).getText());
}(org.openqa.selenium.NoSuchElementException nsee){
}
}
Related
<table>
<tr>
<td>hi</td>
</tr>
</table>
<button id="change" onclick="change()">change</button>
<script>
function change(){
$("table").empty();
var $tr = $("<tr></tr>");
$tr.append( $("<td>bye</td>") );
}
</script>
WebDriver driver=new ChromeDriver(chromeOptions);
WebElement change= driver.findElement(By.id("change"));
change.click();
WebElement td=driver.findElement(By.xpath("//*table/tr/td[1]"));
System.out.println(td.getText());
I expected "bye", but it printed "hi".
How can I get dynamically changed text?
With the use of WebDriverWait explicit waits you can get the initial element text content. Then to wait for that text to no more be presented in that element after the click and then to get the new element text content. As following:
WebDriverWait wait = new WebDriverWait(webDriver, 20);
WebElement td =driver.findElement(By.xpath("//*table/tr/td[1]"));
String oldText = td.getText();
driver.findElement(By.id("change")).click();
wait.until(ExpectedConditions.not(ExpectedConditions.textToBePresentInElementLocated(By.xpath("//*table/tr/td[1]"), oldText)));
System.out.println(td.getText());
I want to click on the Checkbox element which is present in the dynamic web table which has 3 static columns (CheckBox, Description, Link) and dynamic rows.
I'm able to get the exact text of the description of the check box but I'm unable to click on the check box.
Here's the script I tried to achieve my expectation but didn't work. Might be a wrong approach.
WebElement dataTable = driver.findElement(By.id("table_id"));
List<WebElement> TDs = dataTable.findElements(By.tagName("td"));
for(WebElement td : TDs)
{
if (!td.getText().trim().equals("text that i want to click on its checkbox"))
continue;
WebElement particularTd = td.findElement(By.xpath("//*[#type='checkbox']//preceding::input"));
particularTd.click();
}
Could you tell me the right way to click on the check box?
Thanks,
Karunagara Pandi G
I think the td contains only the data.
What you can try is to navigate back to the immediate parent table-row (tr) containing table-data(td). Then searching for the input 'checkbox' there.
<table>
<tr>
<td>
<center><input type='checkbox'/></center>
</td>
<td>
<span>Cell Desc</span>
</td>
<td>
<hyperlink>
</td>
</tr>
</table>
To click the checkbox you can use xpath:
//*[#text='Cell Desc']/ancestor::tr//input
To make the xpath dynamic you can fetch the value and use that value
String value = "Cell Desc";
String xpath = "//*[#text='" + value + "']/ancestor::tr//input";
This worked for me please try the below code:
for(WebElement td : TDs)
{
if (td.getText().contains("Your Text")
td.click;
}
//if you are unable to click with td.click; use below code
for(WebElement td : TDs)
{
if (td.getText().contains("Your Text")
{
WebElement particularTd = td.findElement(By.xpath("//*[#type='checkbox']//preceding::input"));
particularTd.click()
}
}
How can i get all <a> value?
<div id="mCSB_1_container" class="mCSB_container" style="position: relative; top: -985px; left: 0px;" dir="ltr">
<li>
All countries
</li>
<li>
Germany
</li>
<div>
When i use
List<WebElement> allElements = driver.findElements(By.xpath("//div[#id='mCSB_1_container']/li/a"));
for (WebElement element: allElements) {
System.out.println(element.getText());
}
i get empty values.
Try one of below selectors:
By.xpath("//div[#id='mCSB_1_container']/li/a")
By.cssSelector("#mCSB_1_container li a")
By.cssSelector("div[id='mCSB_1_container'] li a")
By.xpath("//*[contains(#id, 'mCSB_1_container')]/li/a")
Or one of this findElements (I don't know if Java contains FindElements extension to IWebElement, but it work in other languages):
List<WebElement> allElements = driver
.findElement(By.id("mCSB_1_container"))
.findElements(By.cssSelector("li a"));
List<WebElement> allElements = driver
.findElement(By.id("mCSB_1_container"))
.findElements(By.tagName("a"));
List<WebElement> allElements = driver
.findElement(By.id("mCSB_1_container"))
.findElements(By.xpath("//li/a"));
Also, if no one works, try to find when breaks, using something like:
By.cssSelector("#mCSB_1_container") //Shoud find the id.
By.cssSelector("#mCSB_1_container li") //Shoud find the li inside the id.
If is unable to find one of they, there is some problem with frames or wait the element display
List<WebElement> allElements = driver.findElements(By.tagName("a"));
for(WebElement element: allElements)
{
System.out.println(element.getText());
}
Jsoup is a great html parser and it can be used with Selenium.
here is the Jsoup web page; https://jsoup.org/
and if you want to get all links from page you can use Jsoup like that;
Document doc = Jsoup.connect("https://jsoup.org/").get();
Elements links = doc.select("a[href]");
for (Element link : links) {
print(link);
}
this code snippet allows you to get all href values of all links. if you want to get all from web page use ;
Elements links = doc.select("a");
instead of
Elements links = doc.select("a[href]");
I have been trying to get the anchor link via WebDriver but somehow, things aren't working as desired and I am not getting the element.
Below is the HTML Structure:
<div id="1">
<table width="100%">
<tr>
<td>.....</td>
<td>
<ul class="bullet">
<li>....</li>
<li>....</li>
<li>
myText
</li> // myText is the text I am searching for
</ul>
</td>
</tr>
</table>
<div>....</div>
</div>
The <li> elements contains anchor tags with links only. No id or any other attribute they contain. The only difference is the text displayed by them and hence, I am passing myText to detect exactly what I need.
And for this, the java code I have been trying is:
driver.get("url");
Thread.sleep() //waiting for elements to get loaded. Exceptional Handling not done.
WebElement divOne = driver.findElement(By.id("1"));
WebElement ul = divOne.findElement(By.className("bullet"));
WebElement anchor = null;
try{
anchor = ul.findElement(By.partialLinkText("myText"));
}catch(NoSuchElementException ex){
LOGGER.warn("Not able to locate tag:" + linkText + "\n");
}
String myLink = anchor.getAttribute("href"); // null pointer exception
I don't understand why is this happening. What is the correct way to do this? Should I use some other method?
driver.findElement(By.xpath("//a[contains(text(),'myTest')]"));
that searches for myTest text. I haven't tried the code, I hope it helps you
you can use any of the below. Should work for you
List<WebElement> anchor = driver.findElements(By.partialLinkText("myText"));
or
driver.findElements(By.linkText("myText"))
or
driver.findElements(By.Xpath("//a[contains(text(),'myText')]"));
String myLink = anchor.getAttribute("href"); // null pointer exception
You are getting exception because you didn't set the anchor value, anchor variable is created and intialized to null and after that it is never updated.
I think the correct code should be below
String myLink = element.getAttribute("href");
Try using a WebDriver instance directly instead of WebElement instance...i.e., use driver instead of ul..
you can try with wait,
element = new WebDriverWait(driver,10).until(ExpectedConditions.visibilityOfElementLocated(By.linkText("myText")));
I've got the following HTML code:
<div class="ui-selectmenu-menu" style="z-index: 1; top: 251px; left: 37px;">
<ul class="ui-widget ui-widget-content ui-selectmenu-menu-dropdown ui-corner-bottom" aria-hidden="true" role="listbox" aria-labelledby="gwt-uid-191-button" id="gwt-uid-191-menu" style="width: 270px; height: auto;" aria-disabled="false" aria-activedescendant="ui-selectmenu-item-999">
<li role="presentation" class="ui-selectmenu-item-selected">
All Applications</li>
<li role="presentation" class="">
Option Alpha</li>
<li role="presentation" class="ui-corner-bottom">
Option Beta</li>
</ul>
</div>
...
<div class="ui-selectmenu-menu"...>...</div>
I'm able to get the WebElement for ui-selectmenu-menu like this (there are many on the page; hence, the use of findElements) :
List<WebElement> dropdowns = driver.findElements(By.className("ui-selectmenu-menu"));
And the ul below it like this:
WebElement ddChild = dropdowns.get(0).findElement(By.className("ui-selectmenu-menu-dropdown"));
I'm even able to grab all the li under the ddChild like this:
List<WebElement> ddOpts = ddChild.findElements(By.xpath("//*[#id='gwt-uid-191-menu']/li[*]"));
But the problem that I can't seem to figure out how to grab the text-value of the <a href="#nogo"... tag under each li element.
I'd like to be able to loop through all the ddOpts and grab the <a href="#nogo"... text values and save them to an ArrayList<String>.
So, for example, my first ArrayList<String> value would contain All Applications, then Option Alpha, then Option Beta, and then jump to the next ul element from the next dropdowns and do the whole process again, all while adding to the ArrayList<String>.
I'm sure its a simple solution but I've got limited experience with Selenium WebDriver.
Thanks!
PS: Is there a simple way to grab the child of a WebElement?
List<WebElement> ddOpts = ddChild.findElements(By.xpath("//*[#id='gwt-uid-191-menu']/li/a"));
ArrayList<String> links = new ArrayList<String>();
for(WebElement we : ddOpts) {
links.add(we.getText();
}
To extract the href attribute of the WebElement (referring to the anchor tag <a> in this example, do this:
List<WebElement> ddOpts = ddChild.findElements(By.xpath("//*[#id='gwt-uid-191-menu']/li/a"));
ArrayList<String> links = new ArrayList<String>();
for(WebElement we : ddOpts) {
// ADD all the href attribute strings to the list
links.add(we.getAttribute("href"));
}
This may also solve your problem:
List<WebElement> dropdowns = driver.findElements(By.className("x-combo-list"));
WebElement ddChild = dropdowns.get(0).findElement(By.className("x-combo-list-inner"));
List<WebElement> ddOpts = ddChild.findElements(By.xpath("//*[#id=\"x-auto-98\"]/div[4]"));
for(WebElement we:ddOpts){
System.out.println(we.getText());
if(we.getText().contains("ROLE_MANAGER")){
we.sendKeys("ROLE_MANAGER");
we.click();
break;
}
}
the below code will select the OptionAlpha in the dropdown of the above HTML code
driver.findElement(By.xpath("//*[#class='ui-selectmenu-menu')).click();
driver.findElement(By.xpath("//*[#class='ui-widget ui-widget-content ui-selectmenu-menu-dropdown ui-corner-bottom']//**[text()='Option Alpha']")).click();
Please try the below code to get all the links in the <a href
List<WebElement> allLis = driver.findElements(By.xpath("//*[#id='gwt-uid-191-menu']/li/a");
// Looping through above list using for-each loop
for(WebElement eachLi : allLis) {
System.out.println(eachLi.getText());
}
Hope this helps.
href="#nogo" is same for all the anchor tags, so it might create ambiguity in selecting the item by the method
dropdowns.findelement(By.linktext("#nogo"));