日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python实时定位_selenium python 一些操作和定位收集

發布時間:2024/10/14 python 71 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python实时定位_selenium python 一些操作和定位收集 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、Select元素

1.打開百度-設置-搜索設置界面,如下圖所示

2.箭頭所指位置,就是 select 選項框,打開頁面元素定位,下方紅色框框區域,可以看到 select 標簽屬性:

3.選項有三個

每頁顯示 10 條

每頁顯示 20 條

每頁顯示 50 條

2、定位select

定位select有多種方法,下面進行一一介紹

2.1 二次定位

1.定位 select 里的選項有多種方式,這里先介紹一種簡單的方法:二次定位

2.基本思路,先定位 select 框,再定位 select 里的選項

完整代碼如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

# coding:utf-8

from seleniumimport webdriver

from selenium.webdriver.common.action_chainsimport ActionChains

driver= webdriver.Firefox()

driver.get("https://www.baidu.com/")

driver.implicitly_wait(20)

mouse= driver.find_element_by_link_text("設置")

ActionChains(driver).move_to_element(mouse).perform()

driver.find_element_by_link_text("搜索設置").click()

s= driver.find_element_by_id("nr")

s.find_element_by_xpath("//option[@value='50']").click()

# 二次定位另外一種寫法

driver.find_element_by_id("nr").find_element_by_xpath("//option[@value='50']").click()

3.還有另外一種寫法也是可以的,把最下面兩步合并成為一步:

driver.find_element_by_id("nr").find_element_by_xpath("//option[@value='50']").click()

2.2 直接定位

1.有很多小伙伴說 firebug 只能定位到 select 框,還能定位里面的選項。

2.用 direbug 定位到 select 后,下方查看元素屬性地方,點 select 標簽前面的+號,就可以展開里面的選項內容了。

3.然后自己寫 xpath 定位或者 css,一次性直接定位到 option 上的內容。

完整代碼如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

# coding:utf-8

from seleniumimport webdriver

from selenium.webdriver.common.action_chainsimport ActionChains

driver= webdriver.Firefox()

driver.get("https://www.baidu.com/")

driver.implicitly_wait(20)

mouse= driver.find_element_by_link_text("設置")

ActionChains(driver).move_to_element(mouse).perform()

driver.find_element_by_link_text("搜索設置").click()

# 直接點位

driver.find_element_by_xpath(".//*[@id='nr']/option[2]").click()

2.3 Select 模塊(index)點位

1.除了上面介紹的兩種簡單的方法定位到 select 選項,selenium 還提供了更高級的玩法,導入 Select 模塊。直接根據屬性或索引定位。

2.先要導入 select 方法:

from selenium.webdriver.support.select import Select

3.然后通過 select 選項的索引來定位選擇對應選項(從 0 開始計數),如選擇第三個選項:select_by_index(2)

完整代碼如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

# coding:utf-8

from seleniumimport webdriver

from selenium.webdriver.common.action_chainsimport ActionChains

from selenium.webdriver.support.selectimport Select

driver= webdriver.Firefox()

driver.get("https://www.baidu.com/")

driver.implicitly_wait(20)

mouse= driver.find_element_by_link_text("設置")

ActionChains(driver).move_to_element(mouse).perform()

driver.find_element_by_link_text("搜索設置").click()

# 通過索引:select_by_index()

s= driver.find_element_by_id("nr")

Select(s).select_by_index(2)

2.4 Select 模塊(value)定位

1.Select 模塊里面除了 index 的方法,還有一個方法,通過選項的 value值來定位。每個選項,都有對應的 value 值,如

每頁顯示 10 條

每頁顯示 20 條

每頁顯示 50 條

2.第二個選項對應的 value 值就是“20”:select_by_value(2)

完整代碼如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

# coding:utf-8

from seleniumimport webdriver

from selenium.webdriver.common.action_chainsimport ActionChains

from selenium.webdriver.support.selectimport Select

driver= webdriver.Firefox()

driver.get("https://www.baidu.com/")

driver.implicitly_wait(20)

mouse= driver.find_element_by_link_text("設置")

ActionChains(driver).move_to_element(mouse).perform()

driver.find_element_by_link_text("搜索設置").click()

# 通過value定位:select_by_value()

s= driver.find_element_by_id("nr")

Select(s).select_by_value(20)

2.5 Select 模塊(text)定位

1.Select 模塊里面還有一個更加高級的功能,可以直接通過選項的文本內容來定位。

2.定位“每頁顯示 50 條”:select_by_visible_text("每頁顯示 50 條")

完整代碼如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

# coding:utf-8

from seleniumimport webdriver

from selenium.webdriver.common.action_chainsimport ActionChains

from selenium.webdriver.support.selectimport Select

driver= webdriver.Firefox()

driver.get("https://www.baidu.com/")

driver.implicitly_wait(20)

mouse= driver.find_element_by_link_text("設置")

ActionChains(driver).move_to_element(mouse).perform()

driver.find_element_by_link_text("搜索設置").click()

# 通過select_by_visible_text定位

s= driver.find_element_by_id("nr")

Select(s).select_by_visible_text("每頁顯示50條")

3.Select 模塊其它方法

1.select 里面方法除了上面介紹的三種,還有更多的功能如下

select_by_index() :通過索引定位

select_by_value() :通過 value 值定位

select_by_visible_text() :通過文本值定位

deselect_all() :取消所有選項

deselect_by_index() :取消對應 index 選項

deselect_by_value() :取消對應 value 選項

deselect_by_visible_text() :取消對應文本選項

first_selected_option() :返回第一個選項

all_selected_options() :返回所有的選項

總結

以上是生活随笔為你收集整理的python实时定位_selenium python 一些操作和定位收集的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。