Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use DesiredCapabilities class through IEDriverServer in Selenium and Python

I have been struggling with a simple python script working in IE11 for some reason it was unable to find any elemnt, however by setting some desired capabilities in a java code it worked:

System.setProperty("webdriver.ie.driver", "C:\\Python27\\IEDriverServer.exe");
DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();
ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);
WebDriver driver = new InternetExplorerDriver(ieCapabilities);//
driver.navigate().to("http://www.google.com");
WebElement search =driver.findElement(By.name("q"));
search.sendKeys("selenium");
search.submit(); 

However, since I already wrote my tests in python I would like to use the same method in python:

caps = DesiredCapabilities.INTERNETEXPLORER
caps['executablePath'] = 'C:\\Python27\\IEDriverServer.exe'
caps['InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS'] = True
browser = webdriver.Ie(capabilities=caps)
browser.get('https://google.com')
search = WebDriverWait(browser,10).until(EC.presence_of_element_located((By.NAME, 'q')))
search.send_keys("selenium")
search.submit()

But for some reason, selenium is still unable to find the search bar, so I'm guessing the equivalent I wrote in python isn't right, can someone point me out to the right direction please?

like image 634
Meryem Avatar asked Oct 16 '22 16:10

Meryem


1 Answers

You have to take care of a couple of facts as follows:

  • Instead of using DesiredCapabilities.INTERNETEXPLORER you need to use DesiredCapabilities.INTERNETEXPLORER.copy().

Note: Always append .copy() on the DesiredCapabilities object to avoid the side effects of altering the Global class instance.

  • As per the The Desired Capabilities implementation executablePath() is not any valid argument. Instead pass the argument executable_path when initializing the WebDriver/WebClient.
  • As you have added the option introduceFlakinessByIgnoringSecurityDomains(), as per the discussion in the blog You're Doing It Wrong: IE Protected Mode and WebDriver @JimEvans clearly mentioned that adding INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS may get you past the initial exception and will allow the test to run in most cases without incident. However using this capability doesn't solve the underlying problem though. If a Protected Mode Boundary is crossed, very unexpected behavior including hangs, element location not working, and clicks not being propagated, could occur.
  • As you are sending a character sequence to the Google Search Box instead of presence_of_element_located() method use element_to_be_clickable() method.

  • Here is your own code with a couple of modifications:

    from selenium import webdriver
    from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    cap = DesiredCapabilities.INTERNETEXPLORER.copy()
    cap['INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS'] = True
    driver = webdriver.Ie(capabilities=cap, executable_path=r'C:\Utility\BrowserDrivers\IEDriverServer.exe')
    driver.get('https://google.com')
    search = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.NAME, 'q')))
    search.send_keys("selenium")
    search.submit()
    

Browser Snapshot

google_search_selenium_ie


Reference

Here you find a detailed discussion on Unexpected error launching Internet Explorer. IELaunchURL() returned HRESULT 80070012 with Selenium 3.13.0: IEDriverServer_x64_3.13.0

like image 168
undetected Selenium Avatar answered Nov 15 '22 04:11

undetected Selenium