2014-12-25 48 views
-1

我無法使用selenium webdriver關閉jquery對話框。這裏是我的代碼。我正在使用Selenium WebDriver版本:2.44.0,Firefox版本:33.請任何想法。無法使用Selenium Webdriver Python關閉彈出對話框(元素當前不可見,因此可能無法與之交互)

from selenium import webdriver 
from selenium.webdriver.common.keys import Keys 
import time 
from selenium.webdriver.support.ui import WebDriverWait 

browser = webdriver.Firefox() 
browser.get('https://122.155.222.114/ev57/login/login.aspx') 
time.sleep(5) 

#I use inspect element in firebug 
#<div style="float: right; cursor: pointer; display: block;" class="dialog-icon-close" role="button">X</div> 

#browser.execute_script('$(".dialog-icon-close").click();') 
#This work but maybe not the selenium way. 

elem = browser.find_element_by_class_name('dialog-icon-close') 
#This produce error 
#ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with 
elem.click() 

編輯:我現在用的這個方法,它的工作原理,但也許還不夠好。

#browser.execute_script('$(".dialog-icon-close").click();') 
#This work but maybe not the selenium way. 
+0

它在一個框架中。 – SiKing

回答

0

編輯:

這將工作。

browser.switchto().alert(); 
browser.find_element_by_class_name('dialog-icon-close') 

//Returns you to your main frame of reference. 
browser.switch_to.default_content() 

編輯元素的IFrame。

下面的代碼用於獲取具有iframe或單獨框架的元素,並且不適用於此問題。

的Python:

browser.switch_to.frame(driver.find_element_by_id("frameid")) 
browser.find_element_by_class_name('dialog-icon-close').click() 

//Returns you to your main frame of reference. 
browser.switch_to.default_content() 

C#:

driver.SwitchTo().Frame(driver.FindElementById("frameid")); 
driver.FindElement(By.Id("dialog-icon-close")).Click(); 
driver.SwitchTo().DefaultContent(); 

爪哇:

driver.switchTo().frame("frameid"); 
driver.findElement(By.id("dialog-icon-close")).click(); 
driver.switchTo().defaultContent(); 

擾流板(懸停鼠標)

注意Java與C#類似,只是某些方法是小寫,而在C#中它們變成了大寫。有趣。

+0

我在firebug中使用inspect檢查了源代碼,jquery框沒有框架。 –

+0

我會在幾分鐘後回來。我打算在jsfiddle.net上用一些彈出框來測試硒。 –

+0

您是否嘗試過使用driver.SwitchTo()。Alert()。Accept;或嘗試driver.SwitchTo()。Alert();然後搜索該元素單擊它,然後driver.SwitchTo()。DefaultContent(); 。 –

相關問題