My xpath =
".//*[#id='ctl00_ctl00_MainContent_ChildContent1_frmClear_dgCashed']/tbody/tr[67]]/td[1]/strong"
where value 67 keeps on changing. How can i use Regular expression so that i can read the value of this element?
Please Need help,Thanks
As I don't know your HTML, it's not completely clear to me what you're trying to achieve. But if you say you want to find row #67 and the total number is also 67, then I assume you are trying to find the last row in the table!?
If this is case, instead of stating tr[67], you should simply use tr[last()].
The below link is similar one as discussed,
how to pass value from valraible in to xpath
public void selectTableRow(String v) throws Exception {
try {
driver.findElement(By.xpath("".//*[#id='ctl00_ctl00_MainContent_ChildContent1_frmClear_dgCashed']/tbody/tr["+v+"]]/td[1]/strong".click();
}
catch (AssertionError Ae)
{
Ae.printStackTrace();
}
}
Related
I'm using maven and selenium for testing in java. I would like to know if there is a way to call a function every time a test fails. I already have a function that takes an screenshot of the browser. I would like to use it every time selenium throws NoSuchElNoSuchElementExeption for example.
Is there an "easy" way to implement this?
Thank you!
You can create a method to find the element and implement a try-catch block inside that to catch the NoSuchElementException. Call your function to take the screenshot inside that catch block.
e.g.
public WebElement findElement(By locator) {
try {
WebElement element = driver.findElement(locator);
return element;
} catch (NoSuchElementException nse) {
// call the function to take a screenshot
}
}
I eventually found this solution to the problem:
https://darrellgrainger.blogspot.com/2011/02/generating-screen-capture-on-exception.html?m=1
Consists on using WebDriverEventListener.
I have a web automation framework set up that works pretty well. I have a constant issue though that when using SendKeys to write to textboxes, quite often a letter gets missed out. So for example, if my dataset is "TestUserName", something like "TestUerName" gets sent example with a missing letter.
This is a big issue for me, as after the web tests concludes successfully I further check if the database was updated properly. So in the above example I would go to the UserName column and expect to find TestUserName, but the test would fail because TestUerName is found instead.
Any ideas please? I am using selenium 2.53.0.
My code below.
public void inputValue (Object [][] valuesFromExcel)
{
for (int rowNow = 0; rowNow < (valuesFromExcel.length); rowNow++)
{
String newValue = valuesFromExcel[rowNow][0].toString();
if (!newValue.equals(""))
{
WebElement currentElement = driver.findElement(By.id(valuesFromExcel[rowNow][1].toString()));
if (currentElement.getTagName().equals("input"))
{
currentElement.sendKeys(newValue);
}
else if (currentElement.getTagName().equals("select"))
{
new Select(currentElement).selectByVisibleText(newValue);
}
}
}
}
Thanks.
Instead of sending as a string, send it as char...
Convert the string to char and send each char one by one to the text box. Yes there will be a performance issue, but it works fine. It will not skip any of the letters
I am trying to locate element which may have different ID at times.
Here is the example :
id = 'greenbay_packers"
id = "Sf_49ers"
Now, is there a way do to some kind of OR operation in find_element method? so that I can use same element locator for test steps?
Also if this is not possible, is there a way to write fail safe routine that try to locate using find_element(:id,'greenbay_packaers") but if fails try find_element(:id,'sf_49ers"). And only fail test if above 2 are not found.
thanks
It is possible with or
//*[(#id='test1') or (#id='test2')]
For second part,
I would suggest you to try try..catch..finally since you have only two conditions to match
try
{
Driver.FindElement(By.Id("ID1"));
}
catch (NoSuchElementException ex)
{
Driver.FindElement(By.Id("ID1"));
}
finally
{
Console.WriteLine("Failed");
}
Written in C#
I have a Selenium test that is supposed to verify that I'm unable to see a button link unless there is a certain amount of information present on a page such as a pagination link for example. Here is my Selenium assert statement:
def test_bottom_links
load_page('orgs/apache_software')
$driver.find_element(:xpath => "//a[#id='btn_orgs_see_all_projects']").element? == false
end
In my mind this makes sense to me but I receive this error when I run my test:
Selenium::WebDriver::Error::NoSuchElementError: Unable to locate element: {"method":"xpath","selector":"//a[#id='btn_orgs_see_all_projects']"}
The above error is what I want as a passing statement. I don't want Selenium to find this element because it should not be there.
I've also tried this and I get the same error:
$driver.find_element(:xpath => "//a[#id='btn_orgs_see_all_projects']").displayed? == false
I was wondering what the correct syntax should be to make this test pass. I've referred to these linksassertNotSomething and List of Selenium Methods. However, these don't have examples of how they are used so I was wondering how someone would write a test like the above. Thanks for any help offered.
Here's a simple boolean check method that should work.
boolean checkForElementPresence(By locator)
{
try {
driver.findElement(locator);
return true;
} catch (NoSuchElementException e) {
return false;
}
}
You can switch to $driver.find_elements that will return an array without raising an exception. Then you can check for the size of the array to make sure it is empty indicating that the element was not found.
I want to check error messages. These error messages appear only when my website encounters a problem.
My problem is that I use findElement in order to check the error message. So when something goes wrong, Selenium finds it, and everything is O.K.
But when it doesn't (meaning - my website is O.K with no problems) - then Selenium indicates that it doesn't find the element, and rises an exception.
Any idea?
you can surround the findElement in a try-catch block, which will do nothing if the element is not found. e.g.
private boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
//or do nothing
}
}
Take a look at the answer Selenium Webdriver NoSuchElementException
It suggests the following (I've adapted it a bit for your needs) :
List<WebElement> errorElements = driver.findElements(By.id("ERROR_ID"));
if (!errorElements.empty()) {
// Tests your errors
}
1.For this you should design your test case in such a way that you writes code to check error message only when you are sure that you will get error message.
2.But the point is why are you checking for error message when you know that there will be no problem and code will run fine.
3.If you doesn't know that error will occur.. You can place the risky code in try block and write a catch block which will find error message and check it.