Selenium 获取文本信息方法+select(定位)
1.通過先定位到具體的元素然后通過text方法獲取文本信息,如獲取控件名稱等
driver.find_element_by_xpath("//div[/h1").text
2.直接通過定位到的元素的屬性直接獲取屬性名稱,如輸入框提示信息等
driver.find_element_by_id("XXX").get_attribute(YYY)
啟發:元素的定位可以先定位到大的顆粒度,在向小的顆粒度定位,例如先定位到table,在定位到table中的行和列
代碼如下:
此處為寫的獲取第一列元素的list的方法
如果得到的文本只為空,而非我們期望的baidu,那么當前定位的元素可能被隱藏了。
1.判斷是否被隱藏 。?driver.find_element_by_xx().is_displayed() ?
打印結果,如果得到 false的結果.那就說明被隱藏了
2.怎么解決?
? ? ? 方法一: ? 修改頁面當前定位元素,或者當前元素父元素的CSS,使元素的is_displayed() ?值為true.
? ? ? 方法二: ?is_displayed() ?為false的元素,依然可以通過getAttribute()方法獲取元素的屬性.?
? ? ?由于webdriver spec的定義,Selenium WebDriver 只會與可見元素交互,所以獲取隱藏元素的文本總是會返回空字符串。?
? ? 可是,在某些情況下,我們需要獲取隱藏元素的文本。這些內容可以使用element.attribute('attributeName'), 通過textContent,?innerText,?innerHTML等屬性獲 取。(劃重點)
- innerHTML?會返回元素的內部 HTML, 包含所有的HTML標簽。?
例如,<div>Hello <p>World!</p></div>的innerHTML會得到Hello <p>World!</p> - textContent?和?innerText?只會得到文本內容,而不會包含 HTML 標簽。?
- textContent?是 W3C 兼容的文字內容屬性,但是 IE 不支持
- innerText?不是 W3C DOM 的指定內容,FireFox不支持
- innerHTML?會返回元素的內部 HTML, 包含所有的HTML標簽。?
1、Select元素
1.打開百度-設置-搜索設置界面,如下圖所示
2.箭頭所指位置,就是 select 選項框,打開頁面元素定位,下方紅色框框區域,可以看到 select 標簽屬性:
<select id="nr" name="NR">
3.選項有三個
<option selected="" value="10">每頁顯示 10 條</option>
<option value="20">每頁顯示 20 條</option>
<option value="50">每頁顯示 50 條</option>
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?selenium?import?webdriver from?selenium.webdriver.common.action_chains?import?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?selenium?import?webdriver from?selenium.webdriver.common.action_chains?import?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?selenium?import?webdriver from?selenium.webdriver.common.action_chains?import?ActionChains from?selenium.webdriver.support.select?import?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 值,如
<select id="nr" name="NR">
<option selected="" value="10">每頁顯示 10 條</option>
<option value="20">每頁顯示 20 條</option>
<option value="50">每頁顯示 50 條</option>
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?selenium?import?webdriver from?selenium.webdriver.common.action_chains?import?ActionChains from?selenium.webdriver.support.select?import?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?selenium?import?webdriver from?selenium.webdriver.common.action_chains?import?ActionChains from?selenium.webdriver.support.select?import?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() :返回所有的選項
------------------------------------------簽名--------------------------------------- 心簡單,世界就簡單,幸福才會生長;心自由,生活就自由,到哪都有快樂。 -------------------------------------------------------------------------------------
轉載于:https://www.cnblogs.com/klb561/p/9332968.html
總結
以上是生活随笔為你收集整理的Selenium 获取文本信息方法+select(定位)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PowerShell开发游戏 ·
- 下一篇: Failed to configure