【问题标题】:Selenium python how to close a pop up window?Selenium python如何关闭弹出窗口?
【发布时间】:2014-02-24 03:13:20
【问题描述】:

场景:

  • 点击顶部导航中的登录链接。
  • 这会打开一个覆盖表单(弹出窗口)
  • 我填写电子邮件 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']

请帮忙!!

【问题讨论】:

  • 从页面添加 HTML:-div class="ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix"> 跨度>

标签: python selenium


【解决方案1】:

首先,您使用的是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.

【讨论】:

  • 谢谢尔基。仍然出现错误。看起来它无法识别它。 saksoff5th.com 是我正在尝试的网站。请参阅下面的错误。Traceback(最近一次调用):文件“C:\Python27\Off5th_Registration”,第 26 行,在 browser.find_element_by_xpath("//*[@id='dwfrm_login_username_d0wweltullmw']")。 click() 文件“C:\Python27\lib\selenium\webdriver\remote\webdriver.py”,第 221 行,在 find_element_by_xpath 中 return self.find_element(by=By.XPATH, value=xpath)
猜你喜欢
  • 2021-03-11
  • 2021-06-21
  • 2019-10-22
  • 1970-01-01
  • 1970-01-01
  • 2017-06-04
  • 2020-06-22
  • 1970-01-01
  • 2018-08-21
相关资源
最近更新 更多