what will be xpath for image in below mention code - selenium

I am trying to finding xpath for image.below is my code.i am getting error unable to locate the element.
driver.findElement(By.xpath("//img[#src='./pics/logo /home.jpg']")).click();
Below mention is my table code. from where i am trying to find xpath for image.
<table cellspacing="0" cellpadding="0" width="600" border="0">
<tbody>
<tr>
<tr>
<td style="vertical-align: top;padding-top:10px;padding-right: 3px;">
<td width="30%" style="vertical-align: top;padding-top:10px;">
<a title="Access to Data (S,g,...)" target="_top" href="./action/updateTabs?tabSet=requestId=1457516682135">
<img border="0" src="./pics/logo/home/EMLogoMini.jpg">
</a>
</td>

I have observed that there is a space in your xpath and the URL is different too..
Use below code:-
driver.findElement(By.xpath("//img[#src='./pics/logo/home/EMLogoMini.jpg']")).click();
Or use cssSelector as below :-
driver.findElement(By.cssSelector("img[src='./pics/logo/home/EMLogoMini.jpg']")).click();
List<WebElement> list=driver.findElements(By.xpath("//img[#src='./pics/logo/home/EMLogoMini.jpg']"));
for(WebElement e : list){
e.click();
}
How to click by different ways:-
If your problem is that the element is scrolled off the screen (and as a result under something like a header bar), you can try scrolling it back into view like this:
private void scrollToElementAndClick(WebElement element) {
int yScrollPosition = element.getLocation().getY();
js.executeScript("window.scroll(0, " + yScrollPosition + ");");
element.click();
}
if you need you could also add in a static offset (if for example you have a page header that is 200px high and always displayed):
public static final int HEADER_OFFSET = 200;
private void scrollToElementAndClick(WebElement element) {
int yScrollPosition = element.getLocation().getY() - HEADER-OFFSET;
js.executeScript("window.scroll(0, " + yScrollPosition + ");");
element.click();
}
If still not work then use JavascriptExecutor
WebElement element= driver.findElement(By."Your Locator"));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", element);
Hope it will help you :)

try to remove spaces and be sure that the URL is same...

Related

Trying to access Parent and then the sibling element in selenium. Throws no element exception

Element structure
//table[#class='table-table-condensed']//tbody//tr...
<td class="ng-binding">TEXT1</td>
<td class="ng-binding">2020-04-17 18:55:58.022</td>
<td>
<span class="label label-default">Not Started</span>
</td>
<td>
<div class="pull-right">
<button class="btn-success"/>
<button class="run"/>
<button class="review"/>
</div>
</td>
Trying to access the button element :
If Text Matches for this Element :
WebElement we = driver.findElement.By(xpath("//table[#class='table-table-condensed']//tbody//tr//td[#class='ng-binding'][1]");
String str = we.text();
if(str.equals("TEXT1"))
{
WebElement ex = we.findElement(By.xpath("./parent::following:://button[#class='run']"));
ex.click();
}
this throws no element found exception, Tried following::sibling too. Can we use both the tags at a time here. How to access the button element.
Also, will following:: tag does not work, if I had to access the below tags in structure:
<td class ="ng-binding">2020-04-17 18:55:58.022</td> ( Need to extract this element to sort the list by Latest date)
AND
<span class ="label label-default">Not Started</span>
Try below solution
wait = WebDriverWait(driver, 10)
elementText= wait.until(EC.element_to_be_clickable((By.XPATH, "//table[#class='table-table-condensed']//tbody//tr/following-sibling::td[contains(text(),'Test1')][contains(#id, "ng-binding")]")))
button=wait.until(EC.element_to_be_clickable((By.XPATH, "//button[#class='run']")))
if(elementText.text=="Test1"):
button.click()
else:
print "Not found!"
Your ./parent::following:://button[#class='run'] is not valid XPath!
The correct form is: axis::node[predicate]. You are not allowed to chain multiple axis together, and you must specify a node! Note that a wilcard * node is still valid.
What you are probably looking for is ./following-sibling::td//button[#class='run'] or possibly ./following::button[#class='run'].
Other approach, you can try to solve this by using tr[contains(., 'TEXT VALUE')]:
String text = "TEXT1";
WebElement element1 = driver.findElement(By.xpath("//table[#class='table-table-condensed']//tr[contains(., '" +text +"')]//td[2]"));
WebElement element2 = driver.findElement(By.xpath("//table[#class='table-table-condensed']//tr[contains(., '" +text +"')]//span"));
WebElement element3 = driver.findElement(By.xpath("//table[#class='table-table-condensed']//tr[contains(., '" +text +"')]//button[#class='run']"));
System.out.println(element1.getText());
System.out.println(element2.getText());
element3.click();

Selenium -- Click on a blank area of an element

<td width="14%" height="90" valign="top" id="cell_saturday1" name="06/02/2018" onclick="onClick(this);">
<table width="100%" height="30" id="table_saturday1"></table>
</td>
Is there any way I can click anywhere on td in above code not containing table with the id table_saturday1?
You can use this Xpath to click on td which is clickable :
//td[#id='cell_saturday1' and #name='06/02/2018' and contains(#onclick,'onClick(this);')]
You can try with javascript executor as given below.
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("document.getElementById('cell_saturday1').click();");
or
WebElement ele = driver.findElement(By.id("cell_saturday1"));
jse.executeScript("arguments[0].click();",ele);
or Using action class
Actions builder = new Actions(driver);
builder.moveToElement(ele, 0, 0).click().build().perform();
To click anywhere within the <td> except the childnode <table> with id as table_saturday1 you can use the following Locator Strategy
Locator Strategy :
cssSelector:
"td#cell_saturday1:not(#table_saturday1)"
xpath:
"//td[#id='cell_saturday1' and not(#id='table_saturday1')]"

How to retrieve the text from an outer element using selenium webdriver?

How to retrieve the text 'caught jake' from the below code using selenium webdriver?
Am able to point to that text using the below xpath but am unable to print the text. :(
//*[#id='full-scorecard']/div[2]/div/table[1]/tbody/tr[3]/td[2]/child::text()
<div class="row">
<div class="large 20 columns">
<table class="batting-table innings" width="100%">
<tbody>
<tr class="tr-heading">
<tr>
<tr class="dismissal-detail" style="display: table-row;">
<td width="2%" />
<td colspan="8">
<b>12.6</b> caught Jake
<b>73/4</b>
<br/>
Using java you can retrieve from following lines:
String text = driver.findElement(By.xpath("//div[#class='large 20 columns']")).getText();
System.Out.print("Text= "+text);
Above line will return all inner tags string's of div element.
If you want to get only 12.6 text, then use below lines:
String text = driver.findElement(By.xpath("//table[#class='batting-table innings']/tr/tr/tr/td[2]/b")).getText();
System.Out.print("Text= "+text);
And be sure that there should be single element which has this xpath 'By.xpath("//table[#class='batting-table innings']/tr/tr/tr/td[2]/b")', otherwise you will get an exception.
depend on your language, you can use following xpath expression:
//div[#class='large 20 columns']/table/tbody/tr/tr/td[2]/child::text()
full Java code:
JavascriptExecutor js = (JavascriptExecutor) driver;
String jsx = "return document.evaluate(\"//*[#id='full-scorecard']/div[2]/div/table[1]/tbody/tr[3]/td[2]/child::text()\", document, null, XPathResult.STRING_TYPE, null).stringValue;";
String objList = js.executeScript(jsx);
System.out.println( (String) objList);

ImageTag exists in the TR?

EDIT:
public bool getImage()
{
IWebElement table = driver.FindElement(By.Id("DIV_ID_1"));
string name = String.Format("//*[contains(text(), \'{0}\')]", 'TEST1');
IWebElement element = table.FindElement(By.XPath(name));
IWebElement parent = element.FindElement(By.XPath(".."));
try
{
IWebElement image = element.FindElement(By.XPath("//img"));
}
catch (NoSuchElementException e)
{
return false;
}
return true;
}
How would I find out if the TEST1 does have Image? in the below html source code, I have tr and within tr i have td and some tr may have image tag and some may not
So, I will be passing name for an example: TEST1 and in returns I will be expecting if the name has Image tag or not.
again, if I pass TEST2 and TEST3 it should return null since it does not have an image tag and where as TEST1 and TEST4 does have Image tag hence it should return me true.
I tried something like this but did not work:
string name = String.Format(".//td[contains(., \'{0}\')]/..//#src", "TEST1");
IWebElement element = driver.FindElement(By.XPath(name));
get this error: after trying the above code...
The xpath expression './/td[contains(., 'TEST1')]/..//#src'
cannot be evaluated or does notresult in a WebElement
Below is the html source code
<div id="DIV_ID_1">
<table id="TBLID1">
<tr>
<td>
TEST1
</td>
<td>
<img id="ctl00" src="../App_Themes/Default/images/phone.gif" />
</td>
</tr>
<tr>
<td>
TEST2
</td>
</tr>
<tr>
<td>
TEST3
</td>
</tr>
<tr>
<td>
TEST4
</td>
<td>
<img id="ctl02" src="../App_Themes/Default/images/phone.gif" />
</td>
</tr>
</table>
</div>
So I would break this up into a few steps.
First get your element:
WebElement element = driver.findElement(By.xpath("//*[contains(text(), 'TEST1')]"));
Then get the parent element:
WebElement parent = element.findElement(By.xpath(".."));
Then check the parent element for an <img> tag:
Try
{
WebElement image = parent.findElement(By.xpath("//img"));
}
catch (NoSuchElementException e)
{
System.out.println("Did not find an image");
}
I'd wrap this in a function that I could then pass in the text to find the image and return the element if it exists.
Something like:
public WebElement getImage(String innerText)
then just pass in TEST1 or TEST2
Using your original XPath, it works fine, executed both in Firefox and Chrome's developer tools and it returns the img. Does it actually return anything or just doesn't return what you'd expect? Does it error saying the element cannot be found?
This is another way of getting it:
//td[normalize-space(text())='TEST1']/../descendant::img/#src

Where to click on this following object(button), for the values to be displayed in Selenium(Webdriver)?

<table id="ext-comp-1389" class="x-btn x-btn-text-icon " cellspacing="0" style="width: auto;">
<tbody class="x-btn-small x-btn-icon-small-left">
<tr>
<tr>
<td class="x-btn-ml">
<td class="x-btn-mc">
<em class="x-btn-split" unselectable="on">
<button id="ext-gen128" class="x-btn-text create" type="button">New</button>
</em>
</td>
<td class="x-btn-mr">
<i> </i>
</td>
</tr>
<tr>
</tbody>
</table>
Above is the way, the New button is present in the HTML file...
The behavior of the button is it has a '+' sign present next to it...Only when it is clicked on the '+' sign, does the list of options display....When it is clicked on anywhere else on the button nothing happens...
I am trying to automate this, using Selenium Webdriver...And below is the conclusive way in which I am clicking on the button...
private static int buttonwidth=24;//value got from firebug computation tab...
private static final int Xoffset = (buttonwidth/2)+6;
private static final int Yoffset = 0;
private static int buttonwidth1=42;
private static final int Xoffset1 = (buttonwidth/2)-6;
private static final int Yoffset1 = 0;
.......
......
.......
WebElement ele = driver.findElement(By.xpath("//*[#id='ext-gen128']"));//new button's id
Actions build = new Actions(driver);
build.moveToElement(ele, Xoffset, Yoffset).click().build().perform();
WebElement ele1 = driver.findElement(By.xpath("//*[#id='ext-comp-1389']/tbody/tr[2]/td[2]/em"));
Actions build1 = new Actions(driver);
build1.moveToElement(ele, Xoffset1, Yoffset1).click().build().perform();
The layout of the buttons are as follows, Width 42 for Em class and 24 for the Button....Snapshots of them as well...
Can anyone please help me with this? Which coordinates and object should I target?
The moveToElement(ele,x,y) method moves the mouse to an offset from the top-left corner of the element.
So I guess you will have to do some calculations to make sure you get the correct coordinates to click on the + symbol.
Best solution for this kind of problem is, go with Sahi..found it easy to use and has a good functionality .. use the command _click(_xy(_cell("New"),-5,5));