Selenium + Python 警告框處理

來源:互聯網
上載者:User

標籤:miss   資訊   locate   儲存   cti   代碼   class   port   setting   

這幾天在學WebDriver的警告框處理,具體做法是使用switch_to_alert()方法定位到alert/confirm/prompt,然後使用text/accept/dismiss/send_keys等方法進行操作。

但是卻一直卡在接受警告框這一步上,在書上給出的例子是百度首頁的搜尋設定快顯視窗,可是代碼運行後,總是在報錯:selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: 搜尋設定(“搜尋設定”此處可能是其他定位資訊)。後來想想,可以能百度首頁有東西改了,我試試在搜尋網頁面進行同樣的操作,結果就可以了!

附上代碼:

from selenium import webdriver

from selenium.webdriver.common.action_chains import ActionChains

import time

 

driver = webdriver.Firefox( )

driver.implicitly_wait (10)

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

driver.find_element_by_id ("kw").send_keys("selenium")

driver.find_element_by_id ("su").click( )

 

#滑鼠移至上方至“設定”連結

#setting = driver.find_element_by_css_selector (‘.pf‘)

setting = driver.find_element_by_link_text ("設定")

ActionChains(driver).move_to_element(setting).perform( )

 

#開啟搜尋設定

driver.find_element_by_link_text ("搜尋設定").click( )

#driver.find_element_by_class_name  ("setpref").click( )

time.sleep(2)

 

# 儲存設定

driver.find_element_by_class_name (‘prefpanelgo‘).click( )

time.sleep(2)

 

#接受警告框

driver.switch_to_alert ( ).accept( )

driver.quit( )

 

Selenium + Python 警告框處理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.