我需要通过点击位于右上角的窗口“X”来关闭网站上的弹出窗口。 这是我代码中缩短、相关的部分:
问题在于,“X”按钮本身没有唯一的标识符。但是,弹出窗口本身具有唯一的标识符。我只需要能够将其流到“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()
问题
为什么我的解决方案不起作用?
我该如何找到“ui-id-3”(位于第3行)并定位到第4行的元素进行单击?
我该如何找到“ui-id-3”(位于第3行)并定位到第5行的元素进行单击? (只是为了知道如何跨多个元素移动)
相关链接:
Python Selenium中的Following-Sibling
在Selenium (Python)中使用XPath选择器“following-sibling :: text()”