2014-02-23 51 views
0

場景:硒蟒蛇如何關閉彈出窗口?

  • 點擊登錄鏈接頂部的導航欄。
  • 這將打開一個覆蓋形式(彈出)
  • 我填寫電子郵件ID,然後選擇單選按鈕(新客戶)
  • 點擊提交
  • 新的覆蓋形式(彈出打開)
  • 我輸入所有信息(名字,姓氏等)並點擊提交
  • 疊加(彈出窗體)打開顯示一條感謝信息。

問題: - 我想點擊這個彈出窗口右上角的'X'來關閉它。

曾嘗試以下 的Xpath:

browser.find_elements_by_xpath('html/body/div[7]/div[1]/a/span').click() 

這給了錯誤

Traceback (most recent call last): 
File "C:\Python27\Off5th_Registration", line 25, in <module> 
browser.find_elements_by_xpath('html/body/div[7]/div[1]/a/span').click() 
AttributeError: 'list' object has no attribute 'click' 

類名試圖

browser.find_element_by_class_name('ui-dialog-titlebar-close ui-corner-all').click() 

這給了錯誤

Traceback (most recent call last): 
File "C:\Python27\Off5th_Registration", line 25, in <module> 
browser.find_element_by_class_name('ui-dialog-titlebar-close ui-corner-all').click() 
File "C:\Python27\lib\selenium\webdriver\remote\webdriver.py", line 341, in find_element_by_class_name 
return self.find_element(by=By.CLASS_NAME, value=name) 
File "C:\Python27\lib\selenium\webdriver\remote\webdriver.py", line 681, in find_element 
{'using': by, 'value': value})['value'] 

請幫助!

+0

添加網頁中的HTML:-div類= 「UI-對話框的標題欄UI的小部件頭UI刀尖所有UI輔助-clearfix」> user3337644

回答

0

首先,您使用的是find_elements_by_xpath,這是返回給您一個元素列表,您應該使用find_element_by_xpath(注意非複數)。其次,我不確定你的xpath是否正確,雖然我不確定,但是我從某個地方記得你必須使用完整的xpath(以/或/開頭)。所以在你的情況下,這將是'//html/body/div[7]/div[1]/a/span'

第三,不支持複合類名稱,在webdriver上下文中,這意味着如果您的類名稱中有空格,則不能選擇by_class_name。

無論如何,嘗試以下操作:

browser.find_element_by_xpath('//a[@class=\"ui-dialog-titlebar-close ui-corner-all\"]').click() 

編輯回答您的評論: 也許你到達那裏得太快?嘗試:

from selenium import webdriver 
from selenium.webdriver.common.by import By 
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC 

    element = WebDriverWait(driver, 60).until(EC.visibility_of_element_located((By.XPath, '//a[@class=\"ui-dialog-titlebar-close ui-corner-all\"]'))) 

Check here.

+0

謝謝Erki。仍然出現錯誤。看起來它不能識別它。 saksoff5th.com是我正在嘗試的網站。請參閱以下錯誤。回傳(最近呼叫的最後一個): 文件「C:\ Python27 \ Off5th_Registration」,第26行,在 browser.find_element_by_xpath(「// * [@ id ='dwfrm_login_username_d0wweltullmw']」)。點擊() find_element_by_xpath中的第221行文件「C:\ Python27 \ lib \ selenium \ webdriver \ remote \ webdriver.py」 返回self.find_element(by = By.XPATH,value = xpath) – user3337644

+0

我編輯了回答 –