Selenium - after page change cannot find element even it is visible - javascript

I am using Selenium and java, after clicking on one button I land on another page and I see the input tag that I am looking in the viewport
after waiting for page to load with
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
in order to get the tag I use the scrollIntoView() and search for the element by id using javascript inside java this way:
js.executeScript("document.getElementById('elementId').scrollIntoView(true);");
but the problem is that document.getElementById('elementId') returns null; I tried it also in the firefox webdriver console with the same result.
If I execute document.getElementById('elementId') on the same page using firefox console but without using Selenium webdriver I get the tag as expected.
Why am I getting this null using Selenium? How to fix it?

Please use the below code before the scrollIntoView() code
driver.switchTo().frame(driver.findElement(By.tagName("iframe")));
IF any element resides under iframe tag you should switch your driver to iframe using above
If you need to switch your driver in default mode then u need to use below code
driver.switchTo().defaultContent()
If the element is under modal then use it
driver.switchTo().frame("ModelFrameTitle");
or
driver.switchTo().activeElement()

Related

Click a button within an iframe - pyppeteer

I am trying to click a button inside an iframe using pyppeteer - the python version of puppeteer
However, I have drawn a blank. A simple page.click('#selector') does not seem to draw results and await page.waitForSelector('#selector') does not pick it up either
Could anyone advise?
The buttons prior to the iframe work fine
Thanks
You need to switch the context to iframe.
There's an example in other thread: Trying to click a button within an iframe with puppeteer
And there's a solution from pptr docs (probably they're similar to python): https://pptr.dev/#?product=Puppeteer&version=v9.0.0&show=api-class-frame

Python selenium process exits after ActionChain onclick().perform()

Please I am not sure what I have done wrong. On the first page I have my code run the following:
next = self.driver.find_element_by_name("checkout_shipping")
actions = ActionChains(self.driver)
actions.move_to_element(next)
actions.click(next).perform()
after clicking next, I want my code to click deliver on the second page:
deliver = self.driver.find_element_by_name("final_shipping_option")
actions_check = ActionChains(self.driver)
actions_check.move_to_element(deliver)
actions_check.click(deliver).perform()
However the second page loads and does nothing. On terminal I get "Process finished with exit code 0" as though everything worked fine when it didn't.
Selenium is a Library that essentially acts as a user. So it loads pages on a browser and manually preforms a click.
I believe your script is finding the correct Element with name "final_shipping_option" but maybe it clicks it too fast for the browser since it's still loading in the Webpage.
Consider trying a time.sleep(x) or something like this:
Wait until page is loaded with Selenium WebDriver for Python
Another possibility is that the name of the element is incorrect, so you're clicking the wrong element? Try to click it via ID, or Class or even XPATH depending on if the page is very dynamic or not..

Elements wont function in Chrome binary driver on a specific page

A specific page in the site I am testing have few buttons that wont register any clicks via selenium Webdriver or manual clicks while opened with chrome binary driver. It works fine while I manually test it in chrome browser.
I tried adding waits, javascript click, action click for selenium with no success.
Webdriver is not throwing an error for clicking/finding element as its doing as it supposed to (I think) but since the page is not responding and not moving forward to the next, I am getting a page object error
Hard to help without specific details, but here's my best attempt:
Potential Problem 1. Make sure the element is not on an iframe, if so here's a link explaning how to fix your code:
How to switch frames - Selenium - Java
Potential Problem 2. Open an incognito/alternate tab, access the same page and check that the CSS Selector/X-Path you are using is existent and pointing to the correct element. If that is not the case, try to correct it by looking at the classes that did not change from the previous Selector to the new.

How to Manage non-JS popups in Selenium?

I am currently working on testing a site and one of the issues I am running into is working with a non Javascript popup.
I have tried using the Selenium Alert interface.
Sample of what I have done
Alert a = WebDriver.switchTo().alert()
alert.accept()
alert.dismiss()
This seems to work for Javascript pop up alerts but not for non javascript pop up alerts. Is there any way to deal with pop ups with Selenium that aren't Javascript based?
Last time I ran into a pop-up like this, it was a frame that was otherwise "invisible." Open the page with your favorite browser, highlight something in the pop-up, right-click it and choose Inspect Element, then follow its XPath. You may have to switch frames a few times in Selenium to get where you need to be.
If its HTML popup then no specific thing needs to be done, just locate required element normally just like you do for normal web page.
But if its saying element not found then there can be 2 cases:
popup is present inside an iframe
before this popup you were inside iframe but that pop up is present in defaultContent View.
Depending on case, use these solutions:
For #1 : driver.switchTo().frame(0); //Here 0 means first iframe, you can use iframe id also
For #2 : driver.switchTo().defaultContent();

How do I programatically click on a bookmarklet that opens a Javascript window and verify the window's contents?

I want to open a specific URL(relatively easy to achieve in Watir/WatiN), then click on a bookmark/bookmarklet which in turn opens a Javascript window in which certain links then appear. I want to be able to verify links' wording and URLs.
The "problem" is having to use IE (7 & 8) and not Firefox which prevents me from using Selenium IDE for instance, and Watir Recorder seem to be unable to cope with Bookmarklet/Bookmark link.
I've tried using Wintask which accomplished this task partially but I'd very much rather use a regular programming language for this task rather than proprietary tool/scripting language.
I think that if you want to use a tool such as Watir or Selenium the only solution will be to execute the bookmarklet JavaScript from your test code. Open an ordinal bookmark is the same as navigating to some URL.
You can get the bookmarklet JavaScript from its properties. Let's take a List All Links bookmarklet as example. The JavaScript for is is:
javascript:WN7z=open('','Z6','width=400,height=200,scrollbars,resizable,menubar');DL5e=document.links;with(WN7z.document){write('<base target=_blank>');for(lKi=0;lKi<DL5e.length;lKi++){write(DL5e[lKi].toString().link(DL5e[lKi])+'<br><br>')};void(close())}
From the script you can see that the opened window name is Z6 - we will need it in our code. Unfortunately I do not know Watir much, so my example is in Selenium 2.0 (aka WebDriver) and it is in Java, but I think that the same can be done in Watir:
WebDriver driver = new InternetExplorerDriver();
// Open Google page
driver.get("http://www.google.com.ua/");
// Search for something
WebElement searchField = driver.findElement(By.name("q"));
searchField.sendKeys("webdriver");
searchField.submit();
// Bookmarklet script, note that javascript: was removed from original booknarklet
String script = "WN7z=open('','Z6','width=400,height=200,scrollbars,resizable,menubar');DL5e=document.links;with(WN7z.document){write('<base%20target=_blank>');for(lKi=0;lKi<DL5e.length;lKi++){write(DL5e[lKi].toString().link(DL5e[lKi])+'<br><br>')};void(close())}";
// Execute bookmarklet script
((JavascriptExecutor) driver).executeScript(script);
// Switch to the newly opened window
driver.switchTo().window("Z6");
// Find all the links in the bookmarklet window
List<WebElement> links = driver.findElements(By.tagName("a"));
// And list their text - you can do anything with them
for (WebElement link : links) {
System.out.println(link.getText());
}
driver.quit();
As I understand you are testing the bookmarklet. If you need to examine the links on the page, you can do this with Selenium (and I believe that Watir is also able to do this :)

Categories