How to select an element based on other element in selenium webdriver - java

I'm newby in selenium automation.
I have one scenario where I have to click on a button based on some title. but the button element looking same for all all rows.
This is the image:
this is html for the same-
<tr class="odd gradeX">
<td class="hide"></td>
<td>My sample Test</td>
<td> Priyank pareek </td>
<td style="width:50px;text-align:center;">01/11/2017</td>
<td style="width:50px;text-align:center;"> Approved </td>
<td style="width:50px;text-align:center;">
<td style="width:100px;">
<a class="btn btnReject red btn-sm btn-outline sbold uppercase" href="javascript:;" onclick="updateTest(this)" data-status="0" data-gui="6438C99F-E166-49DA-8B89-DD0E2EF33A62" title="Reject">
</td>
</tr>
How can i click on the reject button ? please help me

You can use below XPath:
WebElement element = driver.findElement(By.xpath("//a[#title='Reject']"));
or CSS selector
WebElement element = driver.findElement(By.cssSelector("a[title='Reject']"));
If these selectors still matches multiple elements, try:
WebElement element = driver.findElement(By.xpath("//tr[td[text()='My sample Test']]//a[#title='Reject']"));

Try to locate using following xpath -
//td[text()='My sample Test']/following-sibling::td/a[#title='Reject']
Explanation :-
Find the td tag which having text as 'My sample Test'
following-sibling used to navigate the sibling which have reject button

You can search using class name btnReject. In Python, I usually use driver.getElementByClassName("btnReject").click(). You can change them into Java to use.

Related

want to identify element with JSP core<c:if> tag using selenium webdriver using xpath or any other locators

<tr class="odd" role="row">
<td>PRODTUT020</td>
<td>Product for tours</td>
<td>
<td>
<ul class="icons-list">
<c:if test="true">
<li class="text-info-600">
<a href="edit-se-product_mst-90" title="View">
<i class="icon-eye"/>
</a>
</li>
</c:if>
</ul>
</td>
</tr>
Above is the html body. Want to click on the icon-eye element. How do we identify this element using selenium locators?
This XPath,
//tr[td="PRODTUT020"]//i[#class="icon-eye"]
will select i elements with #class attribute value of icon-eye beneath the tr element whose td child has a string value of PRODTUT020.
Depending upon what's more invariant across your general cases, you might change PRODTUT020 to Product for tours -- both work for the case you show.
It avoids having to name the namespaced element by skipping past it via //.
If you want to fetch i inside c:if element you can use below expression
'//*[name()="c:if" and #test="true"]//i'

Scrape all matching contents using XPath function

How do I make sure that my selenium code scrapes all matching contents of my XPath?
Please help me with your ideas.
For example, these are my HTML Tags:
<tr class="1" role="r1">
<td class="c1">
<a href="www.google.com">
</a>
</td>
</tr>
<tr class="2" role="r2">
<td class="c2">
<a href="www.youtube.com">
</a>
</td>
</tr>
<tr class="3" role="c3">
<td class="c3">
<a href="www.facebook.com">
</a>
</td>
</tr>
I want my selenium code to fetch all links from href tag.
So, below is my XPath:
String links = driver.findElement(By.xpath("//tr[#role='cad']//td[#class='c1']//a")).getAttribute("href");
System.out.println(links);
but it fetches only the first href output, i.e. www.google.com.
The desired output is:
www.google.com
www.youtube.com
www.facebook.com
How can I achieve this?
Any array implementation would be better options?
Try Following code:
List<WebElement> elements= driver.findElements(By.xpath("//table/tbody/tr"));
int i =0 ;
while(i<elements.size()){
WebElement childElement = elements.get(i).findElement(By.cssSelector("a"));
System.out.println(childElement.getAttribute("href"));
i++;
}
Try below code.
List<WebElement> links = driver.findElements(By.xpath("//tr/td/a"));
for(int i=0;i<links.size();i++){
System.out.println(links.get(i).getAttribute("href"));
}

Selenium, Java. Need to select ancestor element inside table by Xpath

I have an HTML page containing the following code :
<table class="report" style="width:100%">
<tbody>
<tr>
<th/>
<th>Position Open
<br>
<span class="timestamp">27/7/2016 16:12:12</span>
</br>
</th>
<th>Position closed
<br>
<span class="timestamp">27/7/2016 16:12:42</span>
</br>
</th>
</tr>
<tr>
<td>
<span dir="ltr">EURJPY</span>
</td>
<td>116.098</td>
<td>116.156</td>
</tr>
</tbody>
</table>
On this page I have another table with the same class attribute "report" but only this table contains texts "Position Open" and "Position Closed".
I need to select elements containing the "EURJPY", "116.098" and "116.156" data.
These elements content is changing i.e. instead of "EURJPY" may appear "EURUSD" or "GBPCAD" etc.
I tried the following code:
driver.findElement(By.xpath("//span[text()='Position Open']/ancestor::table[#class='report'](//tr)[2]/td/span")).getAttribute("textContent");
to get the first required field text but got the Invalid selector error.
Your XPath is close but there were a couple issues.
//span[text()='Position Open']/ancestor::table[#class='report'](//tr)[2]/td/span
You are searching for a SPAN that contains the text 'Position Open' when in fact it is a TH that contains the text.
//th[text()='Position Open']/ancestor::table[#class='report'](//tr)[2]/td/span
(//tr) should be corrected to //tr
//th[text()='Position Open']/ancestor::table[#class='report']//tr[2]/td/span
What you want is the text contained in the TD, not the SPAN. If you pull the text from the TD you can get the text you want from all three elements. If you pull the SPAN, then you will also need to pull the last two TDs. This way is just simpler.
...and finally, the TH contains more than just the text you are looking for. Use .contains() to get a match.
//th[text()='Position Open']/ancestor::table[#class='report']//tr[2]/td
So we take that XPath and put it into Java code and we get the below.
List<WebElement> tds = driver.findElements(By.xpath("//th[contains(text(),'Position Open')]/ancestor::table[#class='report']//tr[2]/td"));
for (WebElement td : tds)
{
System.out.println(td.getText());
}
There can be issues matching the text sometimes, use contains instead, try this selector
//th[contains(.,'Position')]/ancestor::table[#class='report']//tr[2]/td/span
You can use this xpath to locate the 3 <td> tags you are interest in
//th[contains(text(),'Position Open')]/ancestor::table//tr[2]/td
Using it will give you list of three elements, you can extract the text from them
List<WebElement> tds = driver.findElement(By.xpath"//th[contains(text(),'Position Open')]/ancestor::table//tr[2]/td");
String currency = tds.get(1).getText(); // this will be EURJPY
tds.get(2).getText(); // 116.098
tds.get(3).getText(); // 116.156

webdrivr click the button which will call Javascript can't work

(1) html:
<table class="title_button" onclick="onSave();" onmouseout="this.className='title_button'; save_moveout();" onmouseover="this.className='title_button_hi'; save_movein();" title="Copy Running-config to Startup-config">
<tbody>
<tr>
<td>
<img id="title_btn_save" src="https://192.168.100.116/image/title_btn_alert.gif">
</td>
<td id="title_save">Save</td>
</tr>
</tbody>
</table>
(2) Webdriver Java:
tried these two ways both can't work.
driver.findElement(By.id("title_save")).click();
driver.findElement(By.xpath("/html/body/div/table/tbody/tr/td/table/tbody/tr/td[8]/table/tbody/tr/td/div/table/tbody/tr/td[2][#id='title_save'")).click();
(3) Exception:
webdriver element is not currently interactable and may not be manipulated.
You are trying to click the td element, but the actual "clickable" element here is the table:
driver.findElement(By.className("title_button")).click();
It looks like element you try to click in disabled and click operation cannot be performed to this element.
Try to perform click operation on another element e.g. on :
driver.findElement(By.css(".title_button tr")).click();

I want to find the xpath for the image

This is the html code
<div class="navBg">
<table id="topnav" class="navTable" cellspacing="0" cellpadding="0" style="-moz-user- select: none; cursor: default;">
<tbody>
<tr>
<td class="logoCell" valign="top">
<td class="separator">
<td class="navItem relative" style="z-index: 99">
<td class="separator">
<td class="navItem relative">
<a class="content tasks" style="border-width: 0" href="/tasks/otasklist.do">
<div class="label" style="z-index:155; ">Tasks</div>
<img class="sizer" width="84" height="93" src="/img/default/pixel.gif? hash=1106906246"/>
<span class="bottomBorder">
I am trying to find the xpath for the image-->
src="/img/default/pixel.gif?hash=1106906246"
I have tried different combinations e:g
//table/tbody/tr/td[5][#class='navItem relative']/a/div[2]/img
I have written the following code too.
WebDriverWait wait= new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Tasks")));
driver.findElement(By.xpath("//table/tbody/tr/td[5][#class='navItem relative']/a/div[2]/img")).click();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
It's identifying the element on web page by firepath but after running the script it's not clicking on the element and the console shows "No Such Element Exception".
Please answer in java lang only.
Can somebody please help me out.???
Thx
I see you are using Selenium. The safest bet is to find the closest parent with an #id attribute, and work your way down from there. Try this: //table[#id='topnav']//img. As alecxe pointed out, depending on how unique the image is in this table, you may need to narrow the XPath down a little more. Something like //table[#id='topnav']//tr[1]//img, or even //table[#id='topnav']//td[contains(#class, 'navItem')]//img.
The XPath you posted will not work, as it has some problems compared to the sample HTML you posted:
tbody may not appear in all browsers
the qualifier [#class='navItem relative'] for the element td[5] is redundant (although this is not exactly a problem)
div[2] does not exists, your HTML sample shows only one div
There are multiple ways to find the img tag. Depending on the uniqueness of the img tag attributes and it's location on the page.
Here's one way to find it, based on the Tasks div:
//table//div[text()='Tasks']/following-sibling::img
You can also rely on the td in which the img is located and check for the sizer class:
//table//td[contains(#class, 'navItem')]/img[#class='sizer']
And so on.

Categories