Selenium-webdriver Advanced Usage

Source: Internet
Author: User

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 ()
    • Expected Conditions
# 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
    • Taking a screenshot
# 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')
    • Using a Firefoxprofile
# Python  from Import  = webdriver. Firefoxprofile ()#  set something on the profile ... Driver = Webdriver. Remote (Desired_capabilities=webdriver. Desiredcapabilities.firefox, BROWSER_PROFILE=FP)
    • Using chromeoptions
# Python  from Import  = webdriver. Chromeoptions ()#  set some optionsdriver = Webdriver. Remote (Desired_capabilities=options.to_capabilities ())
Using a Proxy
    • Internet Explorer
#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)
    • Chrome

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.

    • Firefox
#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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.