![](/img/trans.png)
[英]Selenium WebDriver + PhantomJS + Python - Execute Script and Handle Dialog Box
[英]Switch to web dialog box in selenium webdriver: Python
我想在selenium web驅動程序(Internet Explorer)下處理一個Web對話框。 我正在使用Python
在我的應用程序中,當我單擊Icon時,會打開一個包含一些文本框(Webelements)的Web對話框,我需要在輸入一些文本后單擊“保存”按鈕。 問題是我不知道焦點是否已切換到Web對話框。 這是我的代碼
driver.find_element_by_xpath("//img[contains(@src,'/images/btn_add.gif')]").click()
driver.switch_to_alert()
driver.find_element_by_name("report_cutoff_date").sendkeys("10/31/2010")
這是我得到的錯誤
Traceback (most recent call last):
File "C:\Users\vthaduri\workspace\LDC\test.py", line 14, in <module>
driver.find_element_by_name("report_cutoff_date").sendkeys("10/31/2010")
File "C:\Python27\lib\site-packages\selenium-2.21.2-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 282, in find_element_by_name
return self.find_element(by=By.NAME, value=name)
File "C:\Python27\lib\site-packages\selenium-2.21.2-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 651, in find_element
{'using': by, 'value': value})['value']
File "C:\Python27\lib\site-packages\selenium-2.21.2-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 153, in execute
self.error_handler.check_response(response)
File "C:\Python27\lib\site-packages\selenium-2.21.2-py2.7.egg\selenium\webdriver\remote\errorhandler.py", line 147, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: u'Unable to find element with name == report_cutoff_date'
為了您的信息,webelement具有相同的名稱和截止日期。
有人可以幫我這個。
嘗試這個:
parent_h = browser.current_window_handle
# click on the link that opens a new window
handles = browser.window_handles # before the pop-up window closes
handles.remove(parent_h)
browser.switch_to_window(handles.pop())
# do stuff in the popup
# popup window closes
browser.switch_to_window(parent_h)
# and you're back
我認為問題在於以下代碼 -
driver.switch_to_alert();
您希望切換到執行第一次單擊()操作時出現的另一個對話框。 我認為出現的這個框不是警報 。 您可能必須使用切換到另一個對話框
driver.getWindowHandles();
driver.switchTo().window(handle);
你可以在這里查看一個例子。
你可以嘗試在switchto語句之前放一些延遲/等待。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.