国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Python的Selenium 3 和Selenium 4的寫法區(qū)別

這篇具有很好參考價值的文章主要介紹了Python的Selenium 3 和Selenium 4的寫法區(qū)別。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

1.Python 版本問題:

? ?Selenium 3的話使用Python 3.6.5都可以繼續(xù)編寫,但是到了Selenium 4的時候,python 的版本需要 3.7 或 更高的版本。

2.差異:

? ?Selenium 4 移除了對舊協(xié)議的支持,并在引擎蓋下默認使用 W3C WebDriver 標準。對于大多數(shù)情況,此實施不會影響最終用戶,主要的例外是Capabilities和Actions類。在開發(fā) Selenium 3.x 版本時,實現(xiàn)了對 W3C WebDriver 標準的支持。支持這個新協(xié)議和舊的 JSON 有線協(xié)議。在 3.11 版左右,Selenium 代碼開始符合 W3C 1 級規(guī)范。最新版本的 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-copyselector

driver.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)

文章來源地址http://www.zghlxwxcb.cn/news/detail-620320.html

到了這里,關于Python的Selenium 3 和Selenium 4的寫法區(qū)別的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。如若轉載,請注明出處: 如若內(nèi)容造成侵權/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領支付寶紅包贊助服務器費用

相關文章

  • python之selenium設置瀏覽器為手機模式(開發(fā)者模式)

    1. 啟動手機模式瀏覽器, 手機型號只能選以下范圍。 常用手機型號列表 2. 指定分辨率,自定義寬高 3. 打開瀏覽器的(開發(fā)者模式)。手機模式和開發(fā)者模式可以一起用

    2024年02月12日
    瀏覽(21)
  • 軟件測試/測試開發(fā)/全日制|Python selenium CSS定位方法詳解

    軟件測試/測試開發(fā)/全日制|Python selenium CSS定位方法詳解

    簡介 CSS選擇器是一種用于選擇HTML元素的模式。它允許我們根據(jù)元素的標簽名、類名、ID、屬性等屬性進行選擇。CSS選擇器的語法簡單而靈活,是前端開發(fā)中常用的定位元素的方式。 selenium中的css定位,實際是通過css選擇器來定位到具體元素,css選擇器來自于css語法。CSS定位有

    2024年01月17日
    瀏覽(26)
  • python selenium playwright庫使用教程 破解網(wǎng)頁防止開發(fā)者模式 截取數(shù)據(jù)請求
隱藏chrome

    python selenium playwright庫使用教程 破解網(wǎng)頁防止開發(fā)者模式 截取數(shù)據(jù)請求 隱藏chrome

    下載 chromedriver的版本一定要與Chrome的版本一致,不然就不起作用。 有兩個下載地址: 1、 http://chromedriver.storage.googleapis.com/index.html 2、 CNPM Binaries Mirror 當然,你首先需要查看你的Chrome版本,在瀏覽器中輸入chrome://version/ ?放chromedriver在chrome安裝目錄 ?默認目錄一般為:C:Pro

    2023年04月25日
    瀏覽(27)
  • 關于python+selenium+requests在服務器端開發(fā)多線程并發(fā)程序踩過的一些坑

    最近因為一個需求,著手開發(fā)一款使用selenium+requests進行多線程的測試工具,當然還是基于相對熟悉的python來開發(fā)。由于很久沒寫了,就有很多道理我都懂,一試就出錯的問題,前前后后折騰了幾天總算是開發(fā)完了,這里就把期間遇到的問題做一個記錄,希望可以幫助到有同

    2024年02月09日
    瀏覽(24)
  • Selenium的xpath高級寫法-實用篇

    Selenium的xpath高級寫法-實用篇

    提示:閱讀本章之前,請先閱讀目錄 //div[text()=‘我是子級’]/parent::div[text()=‘我是父級’] //div[text()=‘我是后面一個的兄弟’]/preceding-sibling::div[1] //div[text()=‘我是后面一個的兄弟’]/following-sibling::div[1] //div[contains(text(), ‘包含我這些內(nèi)容,就符合’)] //span[normalize-space(tex

    2024年02月14日
    瀏覽(12)
  • Python中selenium的玩法,小朋友看了都說學會了(1),字節(jié)跳動測試開發(fā)工程師面試題

    Python中selenium的玩法,小朋友看了都說學會了(1),字節(jié)跳動測試開發(fā)工程師面試題

    driver.switch_to.window(current_windows[0]) 參考代碼示例: import time from selenium import webdriver ? driver = webdriver.Chrome() driver.get(“https://www.baidu.com/”) ? time.sleep(1) driver.find_element_by_id(‘kw’).send_keys(‘python’) time.sleep(1) driver.find_element_by_id(‘su’).click() time.sleep(1) ? js = ‘window.open(“h

    2024年04月17日
    瀏覽(95)
  • Python爬蟲基礎之Selenium詳解_python selenium

    Python爬蟲基礎之Selenium詳解_python selenium

    from selenium import webdriver from selenium.webdriver.common.by import By browser= webdriver.Chrome() url = ‘https://www.baidu.com’ browser.get(url) button = browser.find_element(By.ID, ‘su’) print(button) button = browser.find_element(By.NAME, ‘wd’) print(button) button = browser.find_element(By.XPATH, ‘//input[@id=“su”]’) print(button)

    2024年04月15日
    瀏覽(21)
  • python spider 爬蟲 之 Selenium 系列 (-) Selenium

    python spider 爬蟲 之 Selenium 系列 (-) Selenium

    京東的 seckill 秒殺 專區(qū) 用 urllib 是獲取不到的 回顧一下urllib 爬蟲 Selenium Selenium定義 Selenium是一個用于Web應用程序測試的工具 Selenium測試 直接 運行在瀏覽器中,就像真實的用戶在操作一樣 支持通過各種driver ( FireFoxDriver, InternetExplorerDriver、OperaDriver、ChromeDriver)驅動真實瀏

    2024年02月16日
    瀏覽(46)
  • 【Python+Selenium學習系列4】Selenium常用的方法

    目錄 一、前言 二、基本方法 1、send_keys方法---模擬鍵盤輸入 1.1 源代碼 1.2 運行結果 2、text方法---用于獲取文本值 2.1 源代碼 2.2 運行結果 3、get_attribute()方法---用于獲取屬性值 ?3.1 源代碼 3.2 運行結果 ?4、maximize_window()方法---實現(xiàn)瀏覽器窗口最大化 4.1源代碼 4.2運行結果 5、

    2024年03月18日
    瀏覽(31)
  • Selenium實戰(zhàn)之Python+Selenium爬取京東商品數(shù)據(jù)

    Selenium實戰(zhàn)之Python+Selenium爬取京東商品數(shù)據(jù)

    實戰(zhàn)目標:爬取京東商品信息,包括商品的標題、鏈接、價格、評價數(shù)量。 代碼核心在于這幾個部分: 其一:使用元素定位來獲取頁面上指定需要抓取的; 其二:將頁面上定位得到的數(shù)據(jù)永久存儲到本地文件中。 具體來梳理一下從訪問URL開始到爬取數(shù)據(jù)整個流程下來

    2023年04月13日
    瀏覽(27)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領取紅包,優(yōu)惠每天領

二維碼1

領取紅包

二維碼2

領紅包