[Python] Selenium


7. WebDriver API


7.21. Remote WebDriver WebElement

  • source
  • メソッド&フィールド...
    clear()
    click()
    find_element(by='id', value=None)
    .
    .
    .
    location
    location_once_scrolled_into_view
    parent
    rect
    screenshot
    screenshot_as_base64
    screenshot_as_png
    send_keys
    size
    submit()
    tag_name
    text
    value_of_css_property
    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from webdriver_manager.chrome import ChromeDriverManager
    
    driver = webdriver.Chrome(ChromeDriverManager().install())
    driver.get(url)
    
    id = 'id'
    pw = '***'
    
    id_text = browser.find_element_by_id("id")
    pw_text = browser.find_element_by_id("password")
    
    id_text.clear()
    id_text.send_keys(id)
    
    pw_text.clear()
    pw_text.send_keys(pw)
    
    # enter
    pw_text.send_keys(Keys.RETURN)
    
        

    要素の配置(要素の検索)


    find_element_by_id find_element_by_name find_element_by_xpath find_element_by_link_text find_element_by_partial_link_text find_element_by_tag_name find_element_by_class_name find_element_by_css_selector

    To find multiple elements (these methods will return a list):


    複数の要素が見つかったらlistを返します
    find_elements_by_name find_elements_by_xpath find_elements_by_link_text find_elements_by_partial_link_text find_elements_by_tag_name find_elements_by_class_name find_elements_by_css_selector

    Apart from the public methods given above, there are two private methods which might be useful for locating page elements:
    find_element find_elements

    Example usage:
    from selenium.webdriver.common.by import By
    
    driver.find_element(By.XPATH, '//button[text()="Some text"]')
    driver.find_elements(By.XPATH, '//button')
    These are the attributes available for By class:
    ID = "id" XPATH = "xpath" LINK_TEXT = "link text" PARTIAL_LINK_TEXT = "partial link text" NAME = "name" TAG_NAME = "tag name" CLASS_NAME = "class name" CSS_SELECTOR = "css selector"

    すべての要素を検索!find all elements


    検索
  • xpath:find elementby xpath
  • driver.find_elements_by_xpath(".//*")

    find elements by id

    driver.find_elements_by_id("id")

    find element by id

    driver.find_element_by_id("id")

    execute javascript

    driver.execute_script('code')
    
    # scrollTop += 50
    driver.execute_script('return document.getElementsByClassName("y-scroll")[1].scrollTop += 50')
    
    id = browser.execute_script('return info.id')

    get_property

    div = driver.find_elements_by_id("div")
    div[0].get_property("scrollTop")