1.Python 版本问题:
 
   Selenium 3的话使用Python 3.6.5都可以继续编写,但是到了Selenium 4的时候,python 的版本需要 3.7 或 更高的版本。
 
 
2.差异:
 
   Selenium 4 移除了对旧协议的支持,并在引擎盖下默认使用 W3C WebDriver 标准。对于大多数情况,此实施不会影响最终用户,主要的例外是Capabilities和Actions类。在开发 Selenium 3.x 版本时,实现了对 W3C WebDriver 标准的支持。支持这个新协议和旧的 JSON 有线协议。在 3.11 版左右,Selenium 代码开始符合 W3C 1 级规范。最新版本的 Selenium 3 中的 W3C 兼容代码将在 Selenium 4 中按预期工作。 
 
 
3.Capabilities的更新
 
   W3C WebDriver 标准功能列表:
 
-  browserName
-  browserVersion(代替version)
-  platformName(代替platform)
-  acceptInsecureCerts
-  pageLoadStrategy
-  proxy
-  timeouts
-  unhandledPromptBehavior
   Selenium 3的写法:
 
caps = {}
caps['browserName'] = 'firefox'
caps['platform'] = 'Windows 10'
caps['version'] = '92'
caps['build'] = my_test_build
caps['name'] = my_test_name
driver = webdriver.Remote(sauce_url, desired_capabilities=caps)
 
  Selenium 4的写法:
 
from selenium.webdriver.firefox.options import Options as FirefoxOptions
options = FirefoxOptions()
options.browser_version = '92'
options.platform_name = 'Windows 10'
cloud_options = {}
cloud_options['build'] = my_test_build
cloud_options['name'] = my_test_name
options.set_capability('cloud:options', cloud_options)
driver = webdriver.Remote(cloud_url, options=options)
 
 
  4.定位元素的写法:
 
     Selenium 3的写法:
 
driver.find_element_by_class_name("className")
driver.find_element_by_css_selector(".className")
driver.find_element_by_id("elementId")
driver.find_element_by_link_text("linkText")
driver.find_element_by_name("elementName")
driver.find_element_by_partial_link_text("partialText")
driver.find_element_by_tag_name("elementTagName")
driver.find_element_by_xpath("xpath")
 
     Selenium 4的写法:
 
from selenium.webdriver.common.by import By
driver.find_element(By.CLASS_NAME,"xx")
driver.find_element(By.CSS_SELECTOR,"xx")
driver.find_element(By.ID,"xx")
driver.find_element(By.LINK_TEXT,"xx")
driver.find_element(By.NAME,"xx")
driver.find_element(By.PARITIAL_LINK_TEXT,"xx")
driver.find_element(By.TAG_NAME,"xx")
driver.find_element(By.XPATH,"xx")
 
    注:Selenium 3的写法在 Selenium 4中,是使用不了的。
 
 
 5.多位元素定位:
 
    Selenium 3的写法:
 
driver.find_elements_by_class_name("className")
driver.find_elements_by_css_selector(".className")
driver.find_elements_by_id("elementId")
driver.find_elements_by_link_text("linkText")
driver.find_elements_by_name("elementName")
driver.find_elements_by_partial_link_text("partialText")
driver.find_elements_by_tag_name("elementTagName")
driver.find_elements_by_xpath("xpath")
 
   Selenium 4的写法:
 
driver.find_elements(By.CLASS_NAME,"xx") 
# class name 相当于样式容易重复driver.find_elements(By.CSS_SELECTOR,"xx")
# 获取css selector:右击鼠标-检查,定位到元素,
# 在弹出的elements选中的地方鼠标右击-copy-copyselectordriver.find_elements(By.ID,"xx") 
# id 可以唯一定位到一个元素(全局唯一)driver.find_elements(By.LINK_TEXT,"xx") 
# link text 有时候不是一个输入框也不是一个按钮,
# 而是一个文字链接,例如百度搜索界面左上角的新闻,可能重复driver.find_elements(By.NAME,"xx") 
# name 要确保是全局唯一的driver.find_elements(By.PARITIAL_LINK_TEXT,"xx") 
# partial link text 部分链接定位,链接的部分名称,会有重复的可能。driver.find_elements(By.TAG_NAME,"xx") 
# tag name 标签(很多),类似<div>模块,<a>,
# <link>,<span>,<input>,非常容易重复。driver.find_elements(By.XPATH,"xx")
# 全局唯一
# 获取xpath:右击鼠标-检查,定位到元素,在弹出的elements选中的地方鼠标右击-copy-copyxpath
# xpath格式注意事项:双引号之间有双引号的时候,把里面的双引号改成单引号。
# /* 省略了前面的路径
 
 
 6.executable_path
 
    Selenium 3的写法:
 
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option("useAutomationExtension", False)
driver = webdriver.Chrome(executable_path=CHROMEDRIVER_PATH, options=options)
 
    Selenium 4的写法:
 
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option("useAutomationExtension", False)
service = ChromeService(executable_path=CHROMEDRIVER_PATH)
driver = webdriver.Chrome(service=service, options=options)