Selenium not able to find elements - xpath

Selenium is not able to find elements.
I am trying to find an id with PageHeader_logourl
But it's throwing Unable to locate an element with xpath expression.
Am I doing anything wrong
Here is the html code
<DIV id="page" class="page">
<DIV id="wrapper" class="wrapper">
<DIV id="header" class="header">
<TABLE width="100%" height="40" cellspacing="0" cellpadding="0" border="0" ID="Table2">
<TR>
<TD>
<TABLE height="40" cellspacing="0" cellpadding="0" border="0" ID="Table3">
<TR>
<TD><span id="HeaderLogoImage"></span></TD>
<TD valign="middle"><label id="PageHeader_lblProjectName" class="projectname">Scania Trucks – SSS 4 TestR1</label></TD>
Here is the snapshot of my selenium code:
driver.findElement(By.xpath("//input[#name='UserNameInputText']")).sendKeys(username);
driver.findElement(By.xpath("//input[#name='Brand']")).sendKeys(password);
driver.findElement(By.xpath("//input[#name='CmdLogin']")).click();
Assert.assertTrue(isuserloggedin(),"Login failed");
}
public boolean isuserloggedin()
{
boolean flag = false;
if (!driver.findElements(By.id("Cancel")).isEmpty() ||
driver.findElement(By.xpath("//a[#id=['PageHeader_logoUrl']")).isDisplayed());
{
flag = true;
}
return flag;

There seems to be an error of one extra [ in your xpath. You may want to try this "//*[#id='PageHeader_logoUrl']"
You can look for more strategies here

Try search the parent element first:
by.Xpath("//table[#id='Table3']//a[#id='PageHeader_logoUrl']")
Also my suggestion (again) is to search the nearest/ parent element first and debug if you actually get the element that you wanted.

Related

Outlook changes html on office Add-In

I try to write an outlook plugin that inserts a link into the email body. I want to give the link a style in order to look as a button, to give the user a more engaging experience.
The issue I got, is that outlook changes the style on the elements, and the inline style I put on that element is getting lost.
I am inserting the HTML as shown below:
var tablePref = '<table align="left" border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse;margin: 0;"><tr><td border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse;margin: 0;padding: 10px 18px;background:\'';
var backgroundColor = 'green'; //The color is user-given, I put green as an example
var tableSuf = '\'">Some text</td></tr></table>'
var item = Office.context.mailbox.item;
item.body.setAsync(tablePref+backgroundColor+tableSuf,
{
coercionType: Office.CoercionType.Html,
asyncContext: {}
},
function (asyncResult) {
if (asyncResult.status == Office.AsyncResultStatus.Failed)
{
//Show error dialog
}
else
{
//Successfully set data in item body.
}
});
I get the link on the email successfully, and it looks just right, but when I send the email, the HTML structure of this button looks like this instead:
<table class="MsoNormalTable" border="0" cellspacing="0" cellpadding="0" align="left" style="border-collapse: collapse">
<tbody>
<tr>
<td style="background: 'green';">
<p class="MsoNormal" style="mso-element: frame; mso-element-frame-hspace: 2.25pt; mso-element-wrap: around; mso-element-anchor-vertical: paragraph; mso-element-anchor-horizontal: column; mso-height-rule: exactly">
<a href="https://somelink.com" target="_blank" rel="noreferrer">
<span style="text-decoration: none">Some Text</span>
</a>
<!-- o ignored -->
</p>
</td>
</tr>
</tbody>
</table>
Outlook is changing my a elements, and this causes that mail clients render the element with a line below because the style got overridden. Is there any way to avoid Outlook change my HTML or it is a known bug? This is happening across all platforms (OWA, Outlook for Mac 16.21 and Outlook for Windows)
Thanks for any help

Soritng Ascending and descending order after clicking on table header?

I am using Selenium WebDriver with Java.
I have a table where I have to click on header of each column and want to validate whether sorting functionality is working correctly or not. I need to check for both Asc and Desc order.
So I have to take the count of the table first and the get the header text so that I click on each col 1-1 and then need to validate the sorting.
How can achieve my expected result. Below is the HTML source code:
<div>
<table cellspacing="0" rules="all" border="1" id="ctl00_ContentPlaceHolder1_gvClinicalTrait" style="border-color:appworkspace;border-collapse:collapse;">
<tr class="gridTitleBar">
<th scope="col">Date Collected</th><th scope="col">Clinical Trait Data</th><th scope="col">Source</th><th scope="col">Value</th>
</tr><tr class="gridBody" align="left">
<td>11/6/2008</td><td style="width:200px;">A1C</td><td style="width:200px;">d</td><td>6.00</td>
</tr><tr class="gridBody" align="left">
<td>9/17/2008</td><td style="width:200px;">BP</td><td style="width:200px;">e)</td><td>104/54</td>
</tr><tr class="gridBody" align="left">
<td>7/12/2008</td><td style="width:200px;">BP</td><td style="width:200px;">g</td><td>124/56</td>
</tr><tr class="gridBody" align="left">
<td>6/21/2008</td><td style="width:200px;">BP</td><td style="width:200px;">t</td><td>110/72</td>
</tr><tr class="gridBody" align="left">
<td>6/14/2008</td><td style="width:200px;">BP</td><td style="width:200px;">n</td><td>120/70</td>
</tr>
</table>
</div>
How can achieve my expected result. Below is the HTML source code:
you need to have list or array with expected result.
You need to create loop like this for every test:
List<string> expectedValues = new List<string>("12","13","14");
int expectedNumber = expectedValues.Count;
int tdNumber = 2;
for (int i=1; i< expectedNumber; i++)
{
string result = driver.Find(by.xpath("//div[#id ='ctl00_ContentPlaceHolder1_gvClinicalTrait']/tr["+ i +"]/td["+tdNumber+"]")).Text;
if (result != expectedValues[i])
Assert.Fail("Wrong value!")
}
I didnt try this code, but even if its wrong it's will be pretty close to code that you need.

Finding only required links on a webpage using xpath

I have a webpage that has more than hundred links on a webpage. I am trying to see if i can only store the required number of web links and store them in a array and run a loop for verifying data within each individual links.
The number of links are dynamic and change as per the user, so each time the page loads it has to find the specified links.
This is what i currently have under the for loop for each user:
List<WebElement> linksize = driver.findElements(By.xpath(obj.getProperty("totalbookings")));
int linksCount = linksize.size();
String[] linksENR = null;
linksENR = new String[linksCount];
System.out.println(linksCount);
if (linksCount > 0)
{
System.out.println("We have enrollment");
System.out.println(linksCount);
for (int i = 1; i <= linksCount; i++)
{
driver.navigate().to(linksENR[i]);
System.out.println("here");
}
}
else
{
System.out.println("No enrollment record found");
}
The xpath for the elements that i am trying to click for user 1 is and there is one element:
.//*[#id='006g0000007RH1V_00Nb0000009Yc1K_body']/table/tbody/tr[2]/th/a
The xpath for the elements that i am trying to click for user 2 is as follows and there are two elements:
.//*[#id='006g0000007RH1V_00Nb0000009Yc1K_body']/table/tbody/tr[2]/th/a
.//*[#id='006g0000007RH1V_00Nb0000009Yc1K_body']/table/tbody/tr[3]/th/a
This is what i am using to search with the xpath:
totalbookings = .//*[contains(#id, "7RH1V_00Nb0000009Yc1K_body") and contains(#tagName , "a")]
Output:
No enrollment record found
Is there way to uniquely identify only few chosen links with the xpath? Please assist.
Here is the HTML information for the element:
<table class="list" cellspacing="0" cellpadding="0" border="0">
<tbody>
<tr class="headerRow"></tr>
<!--
ListRow
-->
<tr class="dataRow even first" onmouseover="if (window.hiOn){hiOn(this);}" onmouseout="if (window.hiOff){hiOff(this);}" onfocus="if (window.hiOn){hiOn(this);}" onblur="if (window.hiOff){hiOff(this);}">
<td class="actionColumn"></td>
<th class=" dataCell " scope="row">
<a class=" firepath-matching-node" href="/a1Bg0000001ih6M"></a>
</th>
<td class=" dataCell CurrencyElement"></td>
<td class=" dataCell "></td>
<td class=" dataCell "></td>
<td class=" dataCell "></td>
<td class=" dataCell numericalColumn"></td>
<td class=" dataCell "></td>
</tr>
<!--
ListRow
-->
<tr class="dataRow odd last" onmouseover="if (window.hiOn){hiOn(this);}" onmouseout="if (window.hiOff){hiOff(this);}" onfocus="if (window.hiOn){hiOn(this);}" onblur="if (window.hiOff){hiOff(this);}"></tr>
</tbody>
</table>
I would do it more like this:
List<WebElement> pl = driver.findElements(By.xpath(".//a[contains(#id,'totalbookings')]"));
if (pl.size() > 0)
{
System.out.println("We have enrollment. Looping through links...");
for ( WebElement we : pl )
{
we.click();
System.out.println("Clicked link: " + we.toString() );
thisPageObject.verifyPageLoaded();
driver.executeScript("window.history.go(-1)");
org.openqa.selenium.browserlaunchers.Sleeper.sleepTight(1000);
}
} else {}
System.out.println("No enrollment record found");
}
I guess contains(#tagName , "a") is intended to mean that the current element has a descendent element a. But it actually means that the current element has an attribute named tagName, and this attribute's string value contains the letter "a".
This says to me that you are trying to do something moderately complex with a tool that you really know little about. There's no shame in not knowing XPath, but a few hours spent learning the fundamentals will spare you many hours of trial and error and trying to communicate your problem on web forums.
To do what I think you intended to do, you can use the following XPath expression:
.//*[contains(#id, "7RH1V_00Nb0000009Yc1K_body")]//a
That is if you're trying to select the links themselves. If you're trying to select the element whose id you're testing, as your totalbookings XPath expression implies, you could do
.//*[contains(#id, "7RH1V_00Nb0000009Yc1K_body") and .//a]
but that would not select the links, which was your stated goal.
Also, both of the above look for descendants of the current element (whatever that is). With driver.findElements() you don't seem to have a current node, so it would make more sense to remove the . from the beginning of the XPath and just use //*... to make it clear that you're looking for nodes anywhere in the web page, not just in some chosen subtree.

How to XPATH for Table element which have dynamic Ids

I have to get the XPATH for dynamic Ids . The code look like this
<table width="100%" role="presentation">
<tbody>
<tr>
<tr>
<td id="DWT10" role="presentation">
<div id="zl__TV-main__rows" class="DwtListView-Rows" style="height: 130px; width: 377px;">
<div id="zli__TV-main__654" class="RowDouble RowEven " role="treeitem" tabindex="0" aria-label="Unread, hello everyone, Wilkerson, 12:26 AM" aria-posinset="1" aria-level="1">
My XPATH goes like this :
//div[starts-with(#id='zl__CLV-main') and ./div [contains(#aria-posinset,'1')]]
I am getting:
Could not evaluate XPATH error.
XPAth only works if you have a valid XML source file. In your example there are no closing tags.
Your XPATH is invalid. the start-with function must be changed to
//div[starts-with(#id,'zl__CLV-main')
comma instead of =

Even and odd table rows with Razor

I'm using the Razor view engine with MVC 3 and I'm trying to make even and odd rows have different classes in a table.
So far I've got this
#{ var odd = true; }
#foreach(var userLot in Model) {
if (!odd) {
<tr id="lot#userLot.Id" class="even">
else
<tr id="lot#userLot.Id" class="odd">
}
<td>#userLot.Id</td>
<td>#userLot.Description</td>
<td>#userLot.Carat</td>
<td class="averageBid">#userLot.AverageBid</td>
<td class="rank">#userLot.Rank</td>
<td class="currentBid">#userLot.CurrentBid</td>
<td style="width: 200px; height: 30px;" class="tdWithBidInput"><input type="text" style="display: none" /></td>
</tr>
#{ odd = !odd; }
}
This is giving me endless trouble with the stupid view engine unable to figure out what is markup and what is code. I've tried wrapping the tr opening tags in a text directive, but then the stupid view engine moans about the closing tr tags. If I then wrap the closing tr tag in a text directive the stupid view engine moans that the text directive has no opening tag.
Just to be clear, this
<text></ tr></text>
gives an error that the text tag has no matching opening tag. Lovely.
How do I write this so that Razor doesn't give an error?
Please don't recommend a JavaScript solution, I'm trying to get around the Razor issues here.
How about this:
#{ var odd = true; }
#foreach(var userLot in Model) {
<tr id="lot#(userLot.Id)" class="#(odd ? "odd": "even")">
<td>#userLot.Id</td>
<td>#userLot.Description</td>
<td>#userLot.Carat</td>
<td class="averageBid">#userLot.AverageBid</td>
<td class="rank">#userLot.Rank</td>
<td class="currentBid">#userLot.CurrentBid</td>
<td style="width: 200px; height: 30px;" class="tdWithBidInput"><input type="text" style="display: none" /></td>
</tr>
odd = !odd;
}
#( ... ) is a valid and very useful statement.

Resources