首页 > 其他分享 >webdriver.Chrome New API

webdriver.Chrome New API

时间:2024-01-13 17:55:07浏览次数:33  
标签:webdriver elements Chrome driver element API Syntax find

The webdriver.Chrome APIs are simplified in the latest version of Selenium, and both find_element_by_() and find_elements_by_() methods.
We can fix the issue by using the new API methods that are available in Selenium 4.0.0 onwards.

Let us take a simple example to demonstrate this issue.

def run(driver_path):    
    driver = webdriver.Chrome(executable_path=driver_path)    
    driver.get('https://itsmycode.com')    
    button = driver.find_element_by_class_name("code")
    button.click()
run(driver_path)
Output

Selenium Get Element By ID

from selenium.webdriver.common.by import By

# Old API Syntax
element = find_element_by_id("element_id")
elements = find_elements_by_id("element_id")

# New API Syntax
element = driver.find_element(By.ID, "element_id")
elements = driver.find_elements(By.ID, "element_id")
Selenium Get Element By Name

Ezoic

from selenium.webdriver.common.by import By

# Old API Syntax
element = find_element_by_name("element_name")
elements = find_elements_by_name("element_name")

# New API Syntax
element = driver.find_element(By.NAME, "element_name")
elements = driver.find_elements(By.NAME, "element_name")

Selenium Get Element By Link Text

from selenium.webdriver.common.by import By

# Old API Syntax
element = find_element_by_link_text("element_link_text")
elements = find_elements_by_link_text("element_link_text")

# New API Syntax
element = driver.find_element(By.LINK_TEXT, "element_link_text")
elements = driver.find_elements(By.LINK_TEXT, "element_link_text")

Selenium Get Element By Partial Link Text

from selenium.webdriver.common.by import By

# Old API Syntax
element = find_element_by_partial_link_text("element_partial_link_text")
elements = find_elements_by_partial_link_text("element_partial_link_text")

# New API Syntax
element = driver.find_element(By.PARTIAL_LINK_TEXT, "element_partial_link_text")
elements = driver.find_elements(By.PARTIAL_LINK_TEXT, "element_partial_link_text")

Selenium Get Element By Tag Name

from selenium.webdriver.common.by import By

# Old API Syntax
element = find_element_by_tag_name("element_tag_name")
elements = find_elements_by_tag_name("element_tag_name")

# New API Syntax
element = driver.find_element(By.TAG_NAME, "element_tag_name")
elements = driver.find_elements(By.TAG_NAME, "element_tag_name")

Selenium Get Element By Css Selector

from selenium.webdriver.common.by import By

# Old API Syntax
element = find_element_by_css_selector("element_css_selector")
elements = find_elements_by_css_selector("element_css_selector")

# New API Syntax
element = driver.find_element(By.CSS_SELECTOR, "element_css_selector")
elements = driver.find_elements(By.CSS_SELECTOR, "element_css_selector")

Selenium Get Element By xpath

from selenium.webdriver.common.by import By

# Old API Syntax
element = find_element_by_xpath("element_xpath")
elements = find_elements_by_xpath("element_xpath")

# New API Syntax
element = driver.find_element(By.XPATH, "element_xpath")
elements = driver.find_elements(By.XPATH, "element_xpath")

标签:webdriver,elements,Chrome,driver,element,API,Syntax,find
From: https://www.cnblogs.com/abaelhe/p/17962677

相关文章