Selenium cannot target div in iframe only on Chrome - java

I have a div with a class attribute with the value textLayer. I'm trying to get that div.
Here is my code:
WebElement jsiFrame = driver.findElement(By.tagName("iframe"));
driver.switchTo().frame(jsiFrame);
WebElement docViewerElement = driver.find(By.classname("textLayer")
(note: this isn't exactly the code but it's more or less the functions I call)
I also have a timeout where I wait 5 seconds while requesting each element so it's not a matter of the element not existing when I query it). Selenium fails to find it, even though it's very plainly there. What's more, it only fails on Chrome, Firefox finds it without any problems.

Try dynamic xpath with explicit wait.

You can achieve it in this way
driver.switchTo().frame(0)
WebElement docViewerElement = driver.find(By.classname("textLayer")

Related

Is there a better way to programatically select a value from select2 dropdown using selenium WebDriver?

I am using Selenium WebDriver to automate something. It requires filling a form that involves selecting a value from a select2 dropdown. This is the code snippet that I am using-
final By SELECT_DIV = By.id("s2");
click(SELECT_DIV);
final By INPUT = By.cssSelector(".select2-drop-active .select2-input");
waitForVisibilityOfElement(INPUT);
enterCharSequence(INPUT, "someData");
waitForJSandJQueryToLoad(30);//30 seconds
final By LIST_ITEM = By.cssSelector(".select2-drop-active ul.select2-results li.select2-result-selectable");
click(LIST_ITEM);
FYI, there are no unique ids assigned to some of these elements and hence I used css selectors for locating them.
This code works but it sometimes throws a StaleElementReferenceException. This is the error:
org.openqa.selenium.StaleElementReferenceException: Element not found in the cache - perhaps the page has changed since it was looked up
For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html
Selenium version : 2.53
So, I want to know if there is any way I could avoid this. I read a few posts about it but they were not of much help.
Let me know if you need more information. Any help would be appreciated.
'StaleElementReferenceException' means that the element has changed. It is in another div, another span or the its properties changed. Selenium may found it but it changed the very second you tried to click on it.
You need to search for the same element again and wait for it to be clickAble or visible. For example:
WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement button =
wait.until(ExpectedConditions.
elementToBeClickable(By.id("btnLogin")));

Selenium/Java - is it possible to verify that a driver.switchTo().frame(element) was successful?

I have this code:
WebElement iframeElement = driver.findElement(By.xpath(xpIframe));
driver.switchTo().frame(iframeElement);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(xpLucka)));
driver.findElement(xpLucka).click(); //this click fails
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(xpSvara))); //TimeoutException
driver.findElement(xpSvar).click();
driver.findElement(xpSvara).click();
Frequently it fails on the line with the comment //TimeoutException. When I look at the state of the web page in the browser that is left open it is clear that the click on the line before it has failed. This is confusing. The element clearly is there, I find it without problems in the web inspector and the wait.until on the previous line obviously succeeds.
The next thing I want to make sure doesn't fail is the switchTo() statement. How can I verify a switchTo-call?
Note that is also succeeds frequently. I just ran this in a loop 9 times, it failed 5 times "but" succeeded 4 times.
Any other suggestions why this might happen are of course very welcome.
To click() on an element within a <iframe> so you have to:
Induce WebDriverWait for the desired frameToBeAvailableAndSwitchToIt.
Induce WebDriverWait for the desired elementToBeClickable.
You can use the following Locator Strategies:
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("xpIframe")));
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("xpLucka"))).click();
Reference
You can find a couple of relevant discussions in:
Ways to deal with #document under iframe
Is it possible to switch to an element in a frame without using driver.switchTo().frame(“frameName”) in Selenium Webdriver Java?
I faced some difficulties within my automation testing that WebDriverWait is not always working. As you may already know that Thread.sleep(3000); is not a recommended approach to use when it comes to automation testing, however sometimes you must use it. So for the testing purposes i would intentionally use:
Thread.sleep(3000); --> add some time, to make sure it switched to iframe
WebElement iframeElement = driver.findElement(By.xpath(xpIframe));
driver.switchTo().frame(iframeElement);
Thread.sleep(3000);
driver.findElement(xpLucka).click(); --> see if it will click on element
Thread.sleep(3000);
driver.findElement(xpSvar).click();
Thread.sleep(3000);
driver.findElement(xpSvara).click(); --> same for all other elements

Timed out after 10 seconds waiting for presence of element located using Selenium Wedriver

Locator for this is correct, but when running it in Selenium Webdriver, I am getting the same error .
I have used different kinds of waits like Implicit wait, Explicit wait, and Wait for Presence of element
WebElement myDynamicElement = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.xp‌​ath("//div[#id = 'bs-example-navbar-collapse-1']/ul[2]/li[1]")));
driver.findElement(By.xpath("//div[#id = 'bs-example-navbar-collapse-1']/ul[2]/li[1]")).click();
Instead of using 'presenceOfElementLocated(By locator)', use 'visibilityOfElementLocated(By locator)'.
'presenceOfElementLocated(By locator)' only confirms the presence of element in DOM, it does not mean that element is visible or enable. But if you use 'visibilityOfElementLocated(By locator)', then it confirms the element is present in DOM as well as visible. If you are clicking the element you can also use 'elementToBeClickable(By locator)';
Its Shine.com , clicking on Login Link at top right hand side
You should try using By.linkText() to click on Login button as below :-
driver.get("http://www.shine.com/");
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Login"))).click();

How to verify an invisible element on the Web Page

I am testing a link using webdriver, but i am not able to locate where that link(element) is there on the webpage. How to detect that where the given element is present on the webpage?
Can anybody help?
From the Getting Started Documentation, you can use:
WebElement element = driver.findElement(By.name("elementName"));
Where elementName is the name of the element that you are looking for.
Use Ctrl+Shift+I in chrome browser, and try to find that element in the developer's console using Ctrl+F. (You will have to click developer console's screen first before you do Ctrl+F)
U can check the presence of element by using explicit wait
wait.until(ExpectedConditions.visibilityOf(element))
it will return true or false, boolean value can be used.

How to Manipulate Search Bar That's Not Present in HTML Source Using Selenium?

I'm trying to use Selenium (Java) to automate some searches.
When I go to the page and click Inspect Element on the search bar, I see that it has the id searchBox and name q, both of which are potentially useful. Nevertheless, these properties are nowhere to be found in the HTML when I go to View Source.
When I try to use
WebElement search = driver.findElement(By.id("searchBox"));
or
WebElement search = driver.findElement(By.name("q"));
both come back as unable to be found.
How do I proceed with populating the search field then hitting submit (the submit button also is missing in the source page) if I can't find either of these elements?
EDIT:
As requested, here's a more detailed picture of what's wrong:
The URL of the page accessed by WebDriver is http://www.ycharts.com using the line
driver.get("http://www.ycharts.com/login");
If you go to this page with your actual web browser, right click on the search bar and choose Inspect Element, you'll see that its ID is "searchBox" and its name is "q". However, if you go to view the same page's source, you'll see that there's no such element in the HTML. Clearly, this is why WebDriver can't find it.
driver was initiated as follows:
WebDriver driver = new HtmlUnitDriver();
When I try something like
WebElement search = driver.findElement(By.id("searchBox"));`
The exception returned is:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element with ID: searchBox
So, back to the original question: clearly, the element is there, but it's not in the HTML – how do you interact with it?
The problem is being caused by the fact that the search box is added to the html post page load by javascript.
The HTML returned by http://ycharts.com/ does not contain the searchbox, therefore when Selenium thinks the page has finished loading (i.e DOM ready state), there is no search box.
In order to interact with the search box, you need to tell Selenium to wait until the box appears in the DOM.
The easiest way to achieve this is with WebDriverWait;
WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id<locator>));

Categories