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#
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.
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();
}
}
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 am using selenium to perform repetitive tasks on a website - a very useful feature for automating web tasks.
I am stuck on how to perform an action only if certain text is present on the page. Is this possible?
I do not want the lack of text to break the test case - only to bypass the action.
I have the Flow Control plugin too, but havent figured out how to make this do any more.
In Java you can make a method that returns true or false whether an element is present or not :
public boolean isElementPresent(By by) {
try {
return driver.findElement(by) != null;
} catch (NoSuchElementException e) {
return false;
}
}
And concerning the text you want to find you can do :
if(isElementPresent(By.xpath("//*[contains(text(),'Some text')]"))){
// Do your tests here
}
I am using below code to check for element on my web page
private boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
catch (Exception e)
{
return false;
}
}
I need to check in my program if a particular region appears in result as below
isElementPresent(By.xpath(".//*[#id='header']")));
If this is present this function completes quickly but if above is not present then it run for very long.
Could some one please help me in resolving this issue so that this check can be performed quickly?
Here you are missing somethings that is why it is waiting If there is not element. findElement will wait for an element implicitly specified time. so need to set that time to zero in that method.
isElementPresent(WebDriver driver, By by) {
driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
} finally {
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
}
There are 4 important things going on here. In order:
Setting implicity_wait to 0 so that WebDriver does not implicitly wait.
Returning True when the element is found.
Catching the NoSuchElementException and returning False when we discover that the element is not present instead of stopping the test with an exception.
Setting implicitly_wait back to 30 after the action is complete so that WebDriver will implicitly wait in future.
Apparently, it's long to send the exception because your DOM is big and the xpath isn't the fastest way to get an element. But if you want to use xpath, try to put the best path and avoid that kind of function where is substring checking.
Your actual xpath : .//*[#id='header'] takes so long because you check all tags of your DOM. So if put the tag that what you're looking for, example : you want to catch an input. your xpath should start like that //input[#id='1234'] and it will be shorter than looking all tags.