Explicit Waits
#Python fromSeleniumImportWebdriver fromSelenium.webdriver.common.byImport by fromSelenium.webdriver.support.uiImportWebdriverwait#available since 2.4.0 fromSelenium.webdriver.supportImportExpected_conditions as EC#available since 2.26.0FF=Webdriver. Firefox () Ff.get ("http://somedomain/url_that_delays_loading")Try: Element= Webdriverwait (FF, ten). Until (Ec.presence_of_element_located (by.id,"mydynamicelement")))finally: Ff.quit ()
# Python from Import = webdriverwait (driver, ten= Wait.until (ec.element_to_be_clickable (by.id,' Someid')))
Implicit Waits
# Python from Import = webdriver. Firefox () ff.implicitly_wait (# secondsff.get ("http://somedomain/url_that_ Delays_loading"= ff.find_element_by_id ("mydynamicelement" )
Remote Webdriver
# Python from Import = Webdriver. Remote ("http://localhost:4444/wd/hub", Webdriver. DesiredCapabilities.FIREFOX.copy ()) Driver.get ("http://www.google.com" ) Driver.get_screenshot_as_file ('/screenshots/google.png')
# Python from Import = webdriver. Firefoxprofile ()# set something on the profile ... Driver = Webdriver. Remote (Desired_capabilities=webdriver. Desiredcapabilities.firefox, BROWSER_PROFILE=FP)
# Python from Import = webdriver. Chromeoptions ()# set some optionsdriver = Webdriver. Remote (Desired_capabilities=options.to_capabilities ())
Using a Proxy
#Python fromSeleniumImportWebdriverproxy="localhost:8080"#Create a copy of desired capabilities object.Desired_capabilities =Webdriver. DesiredCapabilities.INTERNETEXPLORER.copy ()#Change the proxy properties of this copy.desired_capabilities['Proxy'] = { "Httpproxy":P Roxy,"Ftpproxy":P Roxy,"Sslproxy":P Roxy,"Noproxy": None,"Proxytype":"MANUAL", "class":"Org.openqa.selenium.Proxy", "AutoDetect": False}#You had to use remote, otherwise you'll have the to code it yourself in Python to#dynamically changing the system proxy preferencesDriver = Webdriver. Remote ("Http://localhost:4444/wd/hub", desired_capabilities)
is basically the same as Internet Explorer. It uses the same configuration on the machine as IE does (on Windows). On Mac it uses the System Preference, Network settings. On the Linux it uses (on Ubuntu) System > Preferences > Network Proxy Preferences (alternatively in "/etc/environment" s ET http_proxy). As of this writing it was unknown how to set the proxy programmatically.
#Python fromSeleniumImportWebdriver fromSelenium.webdriver.common.proxyImport*myproxy="host:8080"Proxy=Proxy ({'Proxytype': Proxytype.manual,'Httpproxy': myproxy,'Ftpproxy': myproxy,'Sslproxy': myproxy,'Noproxy':"' #set this value as desired}) Driver= Webdriver. Firefox (proxy=proxy)#For remoteCaps =Webdriver. DesiredCapabilities.FIREFOX.copy () proxy.add_to_capabilities (Caps) Driver= Webdriver. Remote (Desired_capabilities=caps)
Data Driven Testing
#Python#Collection of String valuesSource = open ("Input_file.txt","R") Values=source.readlines () source.close ( )#Execute for loop for each String in the values array forSearchinchValues:driver.get ('http://www.google.com') Driver.find_element_by_name ("Q"). Send_keys (search) driver.find_element_by_id ("btng"). Click () Element= Webdriverwait (Driver, 5). Until (Expectedconditions.presence_of_element_located (By.xpath,"//*[contains (., ' Results ')]")) assertSearchinchElement.text
Selenium-webdriver Advanced Usage