標籤:
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其他定位方式