Webdriver其他定位方式

來源:互聯網
上載者:User

標籤:

1、下拉框的定位

在遇到select下拉框的選擇時,比如:

<select id="nr" name="NR">    <option selected="" value="10">每頁顯示10條</option>    <option value="20">每頁顯示20條</option>    <option value="50">每頁顯示50條</option>

定位方式如下:

1)、需要匯入Select方法

from selenium.webdriver.support.select import Select

2)、定位到下拉框

sel = driver.find_element_by_xpath("/select[@name=‘NR‘]")

3)、選擇下拉框元素

Select(sel).select_by_value(‘20‘)

2、警示框處理

對於在操作過程出出現的警示框,可以使用switch_to_alert()定位,具體的方法有:

text:返回警示中的文字資訊

accept():接受警示框

dismiss():解除警示框

如:

......
alert = driver.switch_to_alert()text = alert.textprint(text)alert.accept()alert.dimiss()

3、當遇到某些元素在<iframe>標籤下時,需先切換到iframe下,以登入163郵箱為例: 

使用switch_to.frame()切換到iframe,然後再定位使用者名稱和密碼的輸入框:

 

......#切換到iframedriver.switch_to.frame("x-URS-iframe") #可以使用iframe的id或namedriver.find_element_by_name("email").clear()driver.find_element_by_name("email").send_keys("user")driver.find_element_by_name("password").clear()driver.find_element_by_name("password").send_keys("password")......

 

除此之外,可以通過switch_to.parent_content()方法跳出當前表單回到離它最近的的表單中,在多表單的情況下,可以使用switch_to_default_content()跳到最外層的表單。

 

4、多視窗切換

 

......#獲得當前控制代碼driver.current_window_handle#獲得所有控制代碼driver.window_handles#切換視窗driver.swith_to.window()

 

 

Webdriver其他定位方式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.