Python/Selenium - 获取一个元素后面的元素

3
我需要通过点击位于右上角的窗口“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的,对语法不是很熟悉,所以会有些错误。为了解决问题,我只能通过发送按键Esc的方式来暂时应付。但我还是想真正了解如何解决这个问题。不确定在我的解决方案中出现了哪些错误:

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行的元素进行单击? (只是为了知道如何跨多个元素移动)


相关链接:

Python Selenium中的Following-Sibling

如何在Python Selenium中查找下一个同级元素?

在Selenium (Python)中使用XPath选择器“following-sibling :: text()”

Python + Selenium WebDriver - 如果同级包含字符串,则获取div值

Python Selenium中使用变量和following-sibling的正确语法是什么?


我怀疑这个弹出窗口是否在其他框架中,请检查框架切换。 - Deepesh kumar Gupta
当你尝试点击关闭按钮时,你会得到什么样的错误? - Breaks Software
如果我尝试使用"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 to execute 'evaluate' on 'Document': The string '//span[@id, 'ui-id-3']/following-sibling::button' is not a valid XPath expression." - Noctsol
@中断软件。路径有效。它会突出显示正确的代码(这里是第4行)。嗯。我该如何进入下一行?也许那样会起作用? - Noctsol
请查看我的回答。 - Breaks Software
显示剩余2条评论
2个回答

3

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()。(我很确定这是错误的) - Noctsol
实际上,那应该可以运行...只是如果没有将点击事件附加到元素上,单击该span可能不会有太多作用。 - Breaks Software

0
你还可以使用find_element()函数来做类似的事情。
##select an element
header = driver.find_elements(By.XPATH, '//h5')[0]
## get the next link
header.find_element(By.XPATH, './/following-sibling::a').get_attribute('outerHTML')

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接