Cannot find visible element - java

I need help in finding unique element to find these text. i don't know why my xpath is not working please check: image attached is the element for the text.
(new WebDriverWait(driver,10)).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//div[#class='"+viz+"']/h2[text()='"+reports+"']")));
wherein
String reports = Overdue Stock Analysis
String viz = dashlet-wrapper
error message is this :
Expected condition failed: waiting for visibility of all elements located by By.xpath:
//div[#class='dashlet-wrapper']/h2[text()='Overdue Stock Analysis']
(tried for 10 second(s) with 500 milliseconds interval)

You can the xpath I have mentioned in the expected condition for finding the element and
if there is only one element which is required to be fetched through the xpath, you should use visibilityOfElementLocated instead of the method visibilityOfAllElementsLocatedBy as visibilityOfAllElementsLocatedBy is used to check the visibility of a element list and not just a single element.
You can use visibilityOfElementLocated like:
(new WebDriverWait(driver,10)).until(ExpectedConditions. visibilityOfElementLocated(By.xpath("//div[#class='"+viz+"']//h2[#data-title='"+reports+"']")));

Related

Find element using explicit wait within another element

I have the following code:
List<MobileElement> messages = driver.findElements (By.id ("body_bubble")); //find all messages
MobileElement lastMessage = messages.get (messages.size () - 1); //get last message
// xpath query to check if the message is delivered
String xpathDeliveryStatus = ".//*[contains(#resource-id, 'delivered_indicator') or contains(#resource-id, 'read_indicator') or contains(#resource-id, 'sent_indicator')]";
MobileElement deliveryStatus = lastMessage.findElement (By.xpath (xpathDeliveryStatus));
I want to do the same except that I want to find the deliveryStatus variable using explicit wait.
So I want to find the deliveryStatus variable inside the lastMessage variable using wait.until (ExpectedConditions.visibilityOfElementLocated (By.locator))
How can I do this?
I don't want to do it using one immense Xpath. Besides, I don't even know how to find the last element with the id body_bubble using Xpath, as the number of these elements is not fixed and always changing.
P.S. For example, in Python we can define WebDriverWait with an element in the constructor instead of driver:
WebDriverWait(webelement, self.timeout)
Unfortunately, it does not work in Java.
Based on the Java documentation on ExpectedConditions, it looks like we can use presenceOfNestedElementLocatedBy to add explicit wait on the child element:
WebDriverWait wait = new WebDriverWait(driver, 15);
WebElement childElem = wait.until(
ExpectedConditions.presenceOfNestedElementLocatedBy(lastMessage, By.xpath(xpathDeliveryStatus)));

how to make #FindAll annotation stop looking for object once the right one is found?

OS: Windows 10
Selenium Version: 3.4
#FINDALL annotation is supposed to match at least one of the given criteria.
Here is the OR :
URL for WebPage : http://store.demoqa.com/
WebElement : -
#FINDALL ({
#findby(xpath = "//input[#name='s']"),
#findby(xpath = "//a[contains(.,'Magic Mouse')]")
})
public WebElement Header__txtSearch;
I am trying to verify element on page with multiple locators.
When I give 2 correct values of XPath, then the driver identifies it quickly on the webpage and returns normally. But when I give the first one as Correct and second one as Incorrect , then it still returns true that element is found but it waits for the object timeout that was provided while initializing the driver(implicit wait).
Isn't there a way where if it finds the first element then it immediately returns us as true instead of going to match the next locator, So that no more time is taken for the test execution to move forward.
How can it be stopped after finding the correct locater info ??
Or is there a workaround for this in which I can use multiple locators for one element so that driver return true as soon as it matches the one locator correctly?
did you try to combine selectors into one by "|"? like:
#findby(xpath = "//input[#name='s']|//a[contains(.,'Magic Mouse')]")
public WebElement Header__txtSearch;
You can use or in the to get the first matching locator.
#findby(xpath = "//input[#name='s'] or //a[contains(.,'Magic Mouse')]")
public WebElement Header__txtSearch;

findElement that is generated HTML during runtime

I'm trying to find an element that is generated from the PCA Predict API, found in this link here. http://www.pcapredict.com/en-gb/address-capture-software/
The code I have at the moment is as follows but it throws an timeout exception due to it not finding any elements. Yet the xpath is correct as I have checked it in developer tools.
By PCA = By.id("inputPCAnywhere");
driver.findElement(PCA).clear();
driver.findElement(PCA).sendKeys(ValidPostcode);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[#class='pcaitem pcafirstitem']")));
driver.findElement(By.xpath("//div[#class='pcaitem pcafirstitem']")).click();
The element is visible on the page, and developer tools only returns one result that that xpath, there is no ID's to find it by.
It looks like that the first item is getting "selected" by default leading to it's class value being equal to the following:
<div class="pcaitem pcafirstitem pcaselected"...>...</div>
All other following results have only pcaitem class, but none have a pcaitem pcafirstitem class value.
In other words, your problem is the strict class match. I would improve the locator to have a partial match on the class attribute. For instance, with a CSS selector:
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".pcaitem.pcafirstitem")));

XPath- Getting Element from a table having dynamic ID

I'm trying to automate my Test Cases using Selenium for an OBIEE application. Now, I need to read a value from a tabular report generated. The problem is, the ID of the last cell where the total is, keeps on changing.
For example- Currently the id is: db_saw_9270_6_1610_0.
After refreshing, the ID becomes something else. The 4 numbers in between (9270) changes. The remaining bit are the same. I'm using the following logic to capture this element:
driver.findElement(By.xpath(".//*[contains(#id, '_6_1610_0')]")).getText();
But, it is returning org.openqa.selenium.NoSuchElementException: Unable to locate element:
Please tell me where did I go wrong and what should I do?
you can try starts-with and substring (as a substitute for xpath 2.0 methdod ends-with):
string xpath = "//*[starts-with(#id, 'db_saw_') and substring(#id, string-length(#id) - 8) = '_6_1610_0']"
driver.findElement(By.xpath(xpath)).getText();
You can try below xpath:-
driver.findElement(By.xpath("//*[starts-with(#id, 'db_saw')]")).getText();
driver.findElement(By.CSSselector("a[id*='_6_1610_0']")).getText();
Note: the a represents a html element. If your id is in a element then you have to replace a by table.
Check this out for more examples with css selector

Problems with element locating with XPath in Selenium-RC

I'm trying to perform very simple automated test. I created XPath selector in a FirePath, here it is:
//a[#href='http://i.yandex.ru/'][span[contains(.,'ledak.e.v#yandex.by')]]
But Selenium-RC can't locate this element. Code is:
final String StrEmailToTest = "ledak.e.v#yandex.by";
String linkEmailSelector = "//a[#href='http://i.yandex.ru/'][span[contains(.,'"+ StrEmailToTest + "')]]";
selenium.isElementPresent(linkEmailSelector);
and it returns "false"
Could you tell me, what am I doing wrong?
UPD. I've uploaded the *.maft - file here: http://depositfiles.com/files/lhcdh2wtl
Don't be afraid, there are some russian characters on the screen.
Shouldn't your XPath be:
"//a[#href='http://i.yandex.ru/']/span[contains(.,'"+ StrEmailToTest + "')]";
My guess is that selenium is looking for the element even before it's loaded. Is it a dynamically loaded/generated element? If so, use waitForElementPresent(). If not, try changing the method of element identification - use id or name and then try to execute it. To make sure your xpath is correct, in the selenium IDE/plugin for firefox, type the path of the element(issue some random command for command field) and click on "Find Element". If it finds, then selenium has no problem finding it, given that the page/element is loaded or generated. If not, you will have to ask Selenium to wait till the element is loaded.

Categories

Resources