Selenium Webdriver IE could not find element - java

I am trying to navigate to www.google.com and send some inputs to search box using Selenium webdriver with Internet Explorer(IE).
static WebDriver webDriver = null;
static DesiredCapabilities IEDesiredCapabilities = DesiredCapabilities.internetExplorer();
System.setProperty("webdriver.chrome.driver", TestConstants.chromeDriverFilePath);
System.setProperty("webdriver.ie.driver", TestConstants.IEDriverFilePth);
IEDesiredCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,
true);
webDriver = new InternetExplorerDriver(IEDesiredCapabilities);
//webDriver = new ChromeDriver();
webDriver.navigate().to("http://www.google.com");
webDriver.findElement(By.name("q")).sendKeys("Venkatesh Kolisetty");
//webDriver.findElement(By.id("lst-ib")).sendKeys("Venkatesh Kolisetty");
This piece of code runs very well when i use Chrome, but throws org.openqa.selenium.NoSuchElementException when IE is used.
This opens required web page in the IE browser which is opened by selenium. The problem is selenium is not able to find any element after page is loaded only when IE is used. For chrome, it finds required elements.
Is there any capability to be added in IEDesiredCapabilities
Kindly see the possibility of providing a programmatic solution instead of changing internet options manually.

Yes this is common issue when you use IE.
Open regedit.exe
Open HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones
So Zones will contain 0,1,2,3,4 and on right hand side three columns will be visible as soon as you click on 0 i.e. Name Type Data
Now in Name column look for 2500 Double click it. Put Value data as 3 and Base as Hexadecimal
You did this for 0.
Now repeat the same steps for 1,2,3,4..
Do this for all i.e. 0,1,2,3,4,5 => Change all 2500's value data to 3.
After that run this code.
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.ie.driver", "D:\\Selenium\\CP-SAT\\IEDriver\\IEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver();
driver.get("www.google.com");
It will run on IE. You need a IEDriverServer.exe as i have shown in path, which will run your IE browser.
Reply to me for further query. I ran the above code in eclipse and it ran successfully.
Happy learning :-)

Issue resolved after adding some required capabilities.
IEDesiredCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
IEDesiredCapabilities.setCapability(InternetExplorerDriver.INITIAL_BROWSER_URL, "http://www.google.com");
IEDesiredCapabilities.internetExplorer().setCapability("ignoreProtectedModeSettings", true);
IEDesiredCapabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
IEDesiredCapabilities.setJavascriptEnabled(true);
//IEDesiredCapabilities.setCapability("requireWindowFocus", true);
IEDesiredCapabilities.setCapability("enablePersistentHover", false);
IEDesiredCapabilities.setCapability("requireWindowFocus", true); is optional

Related

java driver in selenium project

I am using simple project, where you fill in the form. The last part is to click on a element with no id and no text.
I found that i could use java to do it, but how do i initiate the driver in selenium?
Actions builder = new Actions(driver);
builder.moveToElement(knownElement, 10, 25).click().build().perform();
I tried
WebDriver driver = New ChromeDriver();
but that only raised a lot new problems. I am using IntelliJ.
You need chromedriver.exe to initiate selenium web driver.
First download the ChromeDriver. You need to download ChromeDriver for your respective Operating system from this link
//Then set the download location.
System.setProperty("webdriver.chrome.driver", "C://Selenium-java//chromedriver_win32//chromedriver.exe");
//Creating an object of ChromeDriver
WebDriver driver = new ChromeDriver();
//launching the specified URL
driver.get("https://www.google.com/");
Then find your element to be clicked using xpath.
If you say you don't have any id or text for click then use any parent element as reference which you can identify then use xpath axes to identify it.
Then you can click on that element directly.
driver.findElement(By.xpath("//form[#id = 'id']/child::button")).click;

How can I get a screenshot in headless ChromeDriver?

I am trying to capture screenshots in a headless ChromeDriver instance. I can get a proper screenshot without running headless.
I have tried to just follow an example for this, however, the example is not working properly.
public static void main(String[] args) throws Exception{
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setBinary("/Applications/Google Chrome.app/Contents/MacOS/Google Chrome");
chromeOptions.addArguments("--headless");
WebDriver driver = new ChromeDriver(chromeOptions);
driver.navigate().to("https://adobe.com");
File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
// Now you can do whatever you need to do with it, for example copy somewhere
FileUtils.copyFile(srcFile, new File("testscreenshot.png"));
driver.quit();
}
The screenshot the code above generates will be blank white, with a scroll bar on the side. What can I do to capture a screenshot in headless mode? Is this possible?
This behavior was different on Windows, Ubuntu, and Mac. I am currently working on Mac. It was not rendering the page for some reason. This was all I needed before I took my screenshot
new WebDriverWait(driver, 10)

Click is happening some other place on Cart ICON in MSITE from appium code - Chrome browser

I am trying to click on cart icon on top right corner from Appium in chrome browser mobile.
Code to click :
driver.findElement(By.xpath("//a[#href='/viewcart']")).click();
URL : https://www.2gud.com/?cmpid=2G108229
Note: Please open this URL in mobile device and verify.
Error : Code is clicking somewhere else on mobile device.
This is working. Checked in Android 7.1 emulator
driver.findElement(By.xpath("//a[#href='/rv/viewcart']")).click();
public class Demo {
public static WebDriver driver = null;
public static void main(String args[]) throws InterruptedException {
System.out.println("Launching the chrome driver ");
System.setProperty("webdriver.chrome.driver","src\\test\\resources\\drivers\\chromedriver40.exe");
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("androidPackage", "com.android.chrome");
driver = new ChromeDriver(options);
driver.manage().timeouts().pageLoadTimeout(20,TimeUnit.SECONDS);
driver.get("https://www.2gud.com/?cmpid=2G108229");
Thread.sleep(3000);
driver.findElement(By.xpath("//a[#href='/rv/viewcart']")).click();
Thread.sleep(3000);
System.out.println(driver.getTitle());
driver.quit();
}
Try to click using mouse actions or JavaScript executor.
Try using the following code.
Xpath to find the view cart: //a[contains(#href,'viewcart')]
Executing a click via JavaScript has some behaviors of which you should be aware. If, for example, the code bound to the onclick event of your element invokes window.alert(), you may find your Selenium code hanging, depending on the implementation of the browser driver. That said, you can use the JavascriptExecutor class to do this. My solution differs from others proposed, however, in that you can still use the WebDriver methods for locating the elements.
// Assume driver is a valid WebDriver instance that has been properly instantiated elsewhere.
WebElement viewCart = driver.findElement(By.xpath("//a[contains(#href,'viewcart')]"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", viewCart);
You should also note that you might be better off using the click() method of the WebElement interface, but disabling native events before instantiating your driver. This would accomplish the same goal (with the same potential limitations), but not force you to write and maintain your own JavaScript.

Selenium- After click a link,IE opens two windows instead of one window

I'm working on the secured web application. When I click link within frame, it opened another single window where information to be filled.But when I execute this scenario in selenium, it click the link within frame and system display two windows where window1 shows Blank page with title as "Blank Page- window internet explorer' and window2 shows website security certificate with no title.
When I'm doing manually, it showing single window but during automation, it shows two windows.
Note: Application support only IE10.
script:
System.setProperty("webdriver.ie.driver","./tools/IEDriverServer_32.exe");
DesiredCapabilities caps = DesiredCapabilities.internetExplorer();
caps.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
caps.setCapability("ignoreZoomSetting", true);
WebDriver driver = new InternetExplorerDriver(caps);
driver.get(url);
driver.navigate().to("javascript:document.getElementById('overridelink').click()");
Login the application and next step to click link
driver.findElement(By.xpath(".//table[#id='maintable']//a").click();
Please help me on this.
I encountered the exact same issue in IE 10..The issue appears to be resolved when I am setting "nativeEvents" to true using the DesiredCapabilities Class. You may try the same and let us know if it also works for you. Please find code segments for your reference:
DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();
ieCapabilities.setCapability("nativeEvents", true);
WebDriver driver = new InternetExplorerDriver(ieCapabilities);
The 2nd Line seems to do the trick.
The below solution worked
Modifying the registry value , TabProcGrowth to 0 solved the issue-
Go to HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main
TabProcGrowth (right-click) → Modify… → Value data: 0
which version of selenium jar are you using. Try below code...
System.setProperty("webdriver.ie.driver","./tools/IEDriverServer_32.exe"); WebDriver driver = new InternetExplorerDriver();
driver.get(url); driver.navigate().to("javascript:document.getElementById('overridelink').click()‌​");
if this not works.... last option, please Reinstall IE and problme will be fixed.

Test autocomplete with Selenium webdriver

I have a text box in which when I type one letter say 's' , it displays a list of results ( like google search) .
I am using latest selenium webdriver with java.
I have tried
sendKeys("s"),
JavascriptLibrary jsLib = new JavascriptLibrary();
jsLib.callEmbeddedSelenium(driver, "doFireEvent", driver.findElement(By.id("assetTitle")), "onkeyup");
jsLib.callEmbeddedSelenium(driver, "doFireEvent", driver.findElement(By.id("assetTitle")), "onblur");
jsLib.callEmbeddedSelenium(driver, "doFireEvent", driver.findElement(By.id("assetTitle")), "onclick");
jsLib.callEmbeddedSelenium(driver, "doFireEvent", driver.findElement(By.id("assetTitle")), "onmouseup");
driver.findElement(By.id("assetTitle")).sendKeys(Keys.ENTER);
None of these work even after adding wait after each of the steps.
Any suggestions?
Thanks.
Update :-
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
WebElement query = driver.findElement(By.name("q"));
query.sendKeys("s");
driver.findElement(By.xpath("//table[#class='gssb_m']/tbody/tr/td/div/table/tbody/tr/td/span")).click();
driver.findElement(By.name("btnG")).click();
Update 2 : -
WebDriver driver = new FirefoxDriver();
driver.get("http://www.kayak.com/");
WebElement query = driver.findElement(By.name("destination"));
query.sendKeys("s");
Update 3 :-
I tried with Selenium 1 and the fireevent method works by passing parameter as 'keydown'. This should be a temporary workaround for now.
WebDriver driver = new FirefoxDriver();
driver.get("http://www.kayak.com/");
DefaultSelenium sel = new WebDriverBackedSelenium(driver,"http://www.kayak.com/");
sel.type("//input[#id='destination']", "s");
sel.fireEvent("//input[#id='destination']", "keydown");
I found a workaround about this. My problem was:
Selenium inputted 'Mandaluyong' to an auto-suggest location field
The auto-suggest field appears together with the matched option
Then selenium left the auto-suggest drop-down open not selecting the matched option.
What I did was:
driver.findElement(By.name("fromLocation")).sendKeys("Mandaluyong");
driver.findElement(By.name("fromLocation")).sendKeys(Keys.TAB);
This is because on a manual test, when I try to press TAB key, two things were done by the system:
Picks the matched option from the auto-suggest drop-down
Closes the auto-suggest drop-down
I believe you are testing auto-suggest here (not auto-complete)
Steps I follow -
Enter something in the input field
Click on the suggestion you want to choose. (You can find the xpath using some tools like Firebug with Firepath, Chrome, etc.)
Verify the text in the input field is same as expected.
This should be a temporary workaround for now.
WebDriver driver = new FirefoxDriver();
driver.get("http://www.kayak.com/");
DefaultSelenium sel = new WebDriverBackedSelenium(driver,"http://www.kayak.com/");
sel.type("//input[#id='destination']", "s");
sel.fireEvent("//input[#id='destination']", "keydown");

Categories