【问题标题】:Python/Selenium - Getting the element after an elementPython/Selenium - 在元素之后获取元素
【发布时间】:2018-03-01 17:01:28
【问题描述】:

我需要通过按右上角的“X”窗口来关闭网站本身的弹出窗口。这是我的代码中缩短的相关部分:

chromedriver = r'C:\Users\do\Desktop\chromedriver.exe'
browser = webdriver.Chrome(chromedriver)
url = 'Fake.com'

browser.get(url) 
browser.find_element_by_id('imgAttachmentsImg').click()
# I need to close out the window after this

问题在于“X”按钮本身没有唯一标识符。但是,弹出窗口本身确实具有唯一标识符。我只需要能够将它向下传递到 X 按钮。

来自页面的信息:

1. V<div class="ui-dialog ui-widget ui-widget-content ui-corner-all ui-front ui-draggable ui-resizable" tabindex="-1" role="dialog" aria-describedby="attachmentsDialogOverview" aria-labelledby="ui-id-3" style="position: absolute; height: auto; width: auto; top: 239px; left: 102px; display: block;">
  2. <div class="ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix">
    3. <span id="ui-id-3" class="ui-dialog-title">Attachments</span>
   4. V<button type="button" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-icon-only ui-dialog-titlebar-close" role="button" aria-disabled="false" title="close">
       5. <span class="ui-button-icon-primary ui-icon ui-icon-closethick"</span>
       6. <span class="ui-button-text">close</span></button>

我是使用 Python 和 Selenium 的新手。我从 VBA 切换过来,仍然不完全理解所有语法,所以我确实犯了错误!我确实通过让 sendkeys 按 Escape 找到了一个创可贴解决方案。但我试图真正了解如何实际解决这个问题。我不确定我的解决方案犯了什么错误:

browser.find_element_by_xpath("//span[@id, 'ui-id-3']/following-sibling::button").click()

问题

  1. 为什么我的解决方案不起作用?

  2. 如何定位“ui-id-3”(第 3 行)并到达第 4 行的元素以单击它?

  3. 如何找到“ui-id-3”(第 3 行)并到达 第 5 行 上的元素以单击它? (只是为了让我知道如何在多个元素之间移动)


我查看的相关链接:

Following-Sibling Python Selenium

Find next sibling element in Python Selenium?

Using XPath Selector 'following-sibling::text()' in Selenium (Python)

Python + Selenium WebDriver - Get div value if sibling contains string

What is the correct syntax for using a variable and following-sibling in Python Selenium?

【问题讨论】:

  • 我怀疑这个弹窗是否在其他框架,检查框架切换。
  • 同框。想通之后我就换了。我现在(终于)可以与弹出窗口中的其他元素进行交互了。
  • 那么,当您尝试点击关闭按钮时,您会遇到什么样的错误?
  • 如果我尝试使用 "browser.find_element_by_xpath("//span[@id, 'ui-id-3']/following-sibling::button").click()",我得到以下信息:“消息:无效的选择器:无法使用 xpath 表达式定位元素 //span[@id, 'ui-id-3']/following-sibling::button 因为以下错误:SyntaxError: Failed在 'Document' 上执行 'evaluate':字符串 '//span[@id, 'ui-id-3']/following-sibling::button' 不是有效的 XPath 表达式。”
  • 因此,您可以在浏览器开发工具中尝试该 xpath 并查看它不起作用。试试 //span[@id='ui-id-3']/following-sibling::button

标签: python selenium siblings


【解决方案1】:

xpath 有问题。这应该工作:

browser.find_element_by_xpath("//span[@id='ui-id-3']/following-sibling::button").click()

要到达它下面的跨度,您可以使用:

browser.find_element_by_xpath("//span[@id='ui-id-3']/following-sibling::button/span")

顺便说一句,如果您对 CSS 更熟悉,等效路径是:

div#ui-id-3 > button
div#ui-id-3 > button span:nth-of-type(1)

【讨论】:

  • 谢谢。它奏效了,你解释了一切(以及更多)。 css添加也非常好!就像我理解 CSS 部分一样,它应该是这样的...browser.find_element_by_css_selector("div#ui-id-3 > button span:nth-of-type(1)").click()。 (很确定这是错误的)
  • 实际上,这应该可以工作......只是如果没有附加到元素的点击事件,点击该跨度可能不会有太大作用。
猜你喜欢
  • 2022-01-03
  • 2020-05-30
  • 2021-04-05
  • 1970-01-01
  • 1970-01-01
  • 2013-12-28
  • 2016-05-22
  • 1970-01-01
  • 2019-05-11
相关资源
最近更新 更多