Selenium Common API

Source: Internet
Author: User

"' Drive object '
   Http://selenium-python.readthedocs.io/browser = Webdriver. Chrome () browser = Webdriver. Firefox () browser = Webdriver. Edge () browser = Webdriver. PHANTOMJS () browser = Webdriver. Safari () .... such as access page browser = Webdriver. Chrome () browser.get (' https://www.taobao.com ') print (browser.page_source) printing source Browser.close () Close the viewer to find the element sheet An element from selenium import webdriver browser = Webdriver. Chrome () browser.get (' https://www.taobao.com ') # 4 way Equivalent input_by_id = Browser.find_element (by.id, ' Q ') Look for ID q by.id,by.css_selector input_first = browser.find_element_by_id (' q ') lookup id for q Input_second = Browse R.find_element_by_css_selector (' #q ') JQ method lookup id= "q" Input_third = Browser.find_element_by_xpath ('//*[@id = "q"] ') Print (Input_first, Input_second, Input_third) browser.close () find multiple elements from selenium import Webdriver Browser = Webdriver. Chrome () browser.get (' HTtps://www.taobao.com ') # First lis = Browser.find_elements_by_css_selector ('. service-bd li ') # Select SERVICE-BD The LI element # 2 lis = browser.find_elements (By.css_selector, '. service-bd li ') # equivalent of print (LIS) bro Wser.close () ... find_elements_by_name find_elements_by_xpath find_elements_by_tag_name fin D_elements_by_class_name find_elements_by_css_selector Element Interactive operation from selenium import Webdriver Impor T time browser = Webdriver. Chrome () browser.get (' https://www.taobao.com ') input = browser.find_element_by_id (' q ') Input.send_key S (' IPhone ') time.sleep (1) input.clear () Input.send_keys (' iPad ') button = Browser.find_element_ By_class_name (' Btn-search ') Button.Click () execute JavaScript from selenium import webdriver browser = W Ebdriver. Chrome () browser.get (' Https://zhihu.com/explore ') browser.execute_script (' Window.scrollto(0,document.body.scrollheight) ' Browser.execute_script (' alert (' to Botton ') ') gets the attribute logo = Browser.find_el ement_by_id (' Zh-top ') print (logo) print (Logo.get_attribute (' class ')) # Printing class Property Get text Value input = BR Owser.find_element_by_class_name (' Zu-top ') print (input.text) Get ID, location, label name, size Input.id input.locatio N # {y: ', x: '} input.tag_name # button Input.size # {' Height ': $, ' width ': 11} ' # element interactive operation ' from Selenium import Webdriverimport Timebrowser = Webdriver. Chrome () browser.get (' https://taobao.com ') input = browser.find_element_by_id (' q ') Input.send_keys (' IPhone ') Time.sleep (1) input.clear () Input.send_keys (' iPad ') button = Browser.find_element_by_class_name (' Btn-search ') Button.Click () "# executes JavaScript ' from selenium import webdriverbrowser = Webdriver. Chrome () browser.get (' Https://zhihu.com/explore ') # Roll to the bottom Browser.execute_script (' Window.scrollto (0, Document.body.scrollHeight) # Browser.execute_script (' Alert (')I Love You ') ' ' # iframe switch use (iframe within the page) ' Import timefrom Selenium import Webdriverfrom selenium.common.exceptions Import Nosuchelementexceptionbrowser = Webdriver. Chrome () url = ' http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable ' browser.get (URL) browser.switch_ To.frame (' Iframeresult ') # switch to this iframesource = Browser.find_element_by_css_selector (' #draggable ') print (source) Try: # in the iframe of the Web page can not find elements outside the current IFRAME, need to switch logo = Browser.find_element_by_class_name (' logo ') # except:except Nosuchelementexception as E:print (' No Logo ') # Toggle iframe (Switch to parent iframe) browser.switch_to.parent_frame () Logo = Browser.fin D_element_by_class_name (' logo ') print (logo) print (logo.text) "# Waiting ... # hermit waits ' ' from selenium import webdriverbrowser = Webdriver. Chrome () browser.implicitly_wait (10) # When the element is not found, it will wait 10 seconds for the search, if there is no browser.get (' Https://www.zhihu.com/explore ') input = Browser.find_element_by_class_name (' zu-top-add-question ') print (input) ' # Display wait ' from selenium import Webdriverfrom Selenium.webdrivEr.common.by Import byfrom selenium.webdriver.support.ui import webdriverwaitfrom selenium.webdriver.support Import Expected_conditions as Ecbrowser = Webdriver. Chrome () browser.get (' https://www.taobao.com ') wait = webdriverwait (browser, ten) input = Wait.until (ec.element_to_be_ Clickable ((By.css_selector, '. Btn-search ')) print (input) "" Displays the conditions of the wait http://selenium-python.readthedocs.io/ Installation.htmltitle_is title is a content Title_contains header contains a content presence_of_element_located ((by.id, ' P ')) element loaded out, passed in the locating tuple, ( By.id, ' P ') the visibility_of_element_located element is visible, the incoming locating tuple visibility_of visible, and the incoming element object presence_of_all_elements_located All elements are loaded out text_to_be_present_in_element an element literal contains a word text_to_be_present_in_element_value an element value contains a word frame_to_be_ Available_and_switch_to_it ifram load and toggle invisibility_of_element_located element Invisible element_to_be_clickable element can be clicked Staleness_ In determining whether an element is still in the DOM, it is possible to determine whether the page has been refreshed element_to_be_selected elements can be selected, the incoming positioning tuple element_located_to_be_selected elements can be selected, passed in the locating tuple element _selection_state_to_be pass in the element object and state, equal returns True, otherwise returns falseelemenT_located_selection_state_to_be incoming locating tuples and states, equal returns True, otherwise returns whether falsealert_is_present appears alert ' # Browser forward back ' import Timefrom Selenium Import webdriverbrowser = Webdriver. Chrome () browser.get (' https://www.baidu.com ') browser.get (' https://www.taobao.com ') browser.get (' https:// Www.python.org ') Browser.back () # Back Time.sleep (1) Browser.forward () # Forward Browser.close () ' # Cookies get and set ' from Selenium Import Webdriverbrowser = Webdriver. Chrome () browser.get (' Https://www.zhihu.com/explore ') # Get Print (Browser.get_cookies ()) # Add Browser.add_cookie ({' Name ': ' Name ', ' domain ': ' www.zhihu.com ', ' value ': ' Germey '} ' Print (Browser.get_cookies ()) # Delete Browser.delete_all_ Cookies () print (Browser.get_cookies ()) ' # tab (open multiple Pages) ' Import timefrom Selenium import Webdriverbrowser = Webdriver. Chrome () browser.get (' https://www.baidu.com ') browser.execute_script (' window.open () ') print (browser.window_ Handles) # There are currently several viewer Windows Browser.switch_to_window (Browser.window_handles[1]) # Toggle the second browser.get (' https:// Www.taobao.com ') time.sleep (1) browser.Switch_to_window (Browser.window_handles[0]) # Toggle the first browser.get (' https://python.org ') ' # exception handling ' ID does not exist exception from Selenium import webdriverfrom selenium.common.exceptions import timeoutexception, Nosuchelementexceptionbrowser = Webdriver. Chrome () try:browser.get (' https://www.baidu.com ') except Timeoutexception:print (' Time Out ') Try:browser.find_elem ent_by_id (' hello ') except Nosuchelementexception:print ("No Element") finally:browser.close () ' from multiprocessing Import Pool

  

Selenium Common API

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.