我需要点击页面上的Ok按钮(下面是HTML代码片段(。按钮本身没有Id,其XPath也发生了更改。它可以是以下任何一种:
/html/body/div[3]/div[3]/div/button
/html/body/div[4]/div[3]/div/button
/html/body/div[5]/div[3]/div/button
我在页面中能找到的唯一稳定的定位器是包含按钮作为子div的div之前的div的noresultsfoundId。
我无法猜测我可以使用哪个Selenium定位器来可靠地点击按钮。
<div class="ui-dialog ui-widget ui-widget-content ui-corner-all ui-front ui-dialog-buttons ui-draggable ui-resizable" tabindex="-1" role="dialog" aria-describedby="noresultsfound" aria-labelledby="ui-id-1" style="position: absolute; height: auto; width: 300px; top: 850px; left: 365px; display: block;">
<div class="ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix"><span id="ui-id-1" class="ui-dialog-title">Attenzione</span><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" style="display: none;"><span class="ui-button-icon-primary ui-icon ui-icon-closethick"></span><span class="ui-button-text">close</span></button></div>
<div id="noresultsfound" class="ui-dialog-content ui-widget-content" style="width: auto; min-height: 0px; max-height: none; height: auto;">
<p>Nessun risultato trovato</p>
</div>
<div class="ui-dialog-buttonpane ui-widget-content ui-helper-clearfix">
<div class="ui-dialog-buttonset"><button type="button" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button" aria-disabled="false"><span class="ui-button-text">Ok</span></button></div>
<p style="border-top: 1px solid #bcbcbc; margin-top:10px; padding-top:5px;color:#054a6c;font-style:italic;font-weight:bold;font-size:0.7em;margin-bottom: 0px;">Il portale è progettato per una visualizzazione ottimale con i seguenti browser:<br>Internet Explorer (V9), Chrome (V 35.0.1916.114m) e Firefox (V28 e29)</p>
</div>
<div class="ui-resizable-handle ui-resizable-n" style="z-index: 90;"></div>
<div class="ui-resizable-handle ui-resizable-e" style="z-index: 90;"></div>
<div class="ui-resizable-handle ui-resizable-s" style="z-index: 90;"></div>
<div class="ui-resizable-handle ui-resizable-w" style="z-index: 90;"></div>
<div class="ui-resizable-handle ui-resizable-se ui-icon ui-icon-gripsmall-diagonal-se" style="z-index: 90;"></div>
<div class="ui-resizable-handle ui-resizable-sw" style="z-index: 90;"></div>
<div class="ui-resizable-handle ui-resizable-ne" style="z-index: 90;"></div>
<div class="ui-resizable-handle ui-resizable-nw" style="z-index: 90;"></div>
</div>
到目前为止,我的代码如下:
try: # things are good when the following error modal window is NOT found
digram_notfound = DRIVER_WAIT.until(
EC.visibility_of_element_located((By.ID, "noresultsfound"))
)
if digram_notfound:
click_xpath(driver, "/html/body/div[4]/div[3]/div/button")
except TimeoutException:
do useful stuff here
但正如我所说,它在XPath不同的页面上失败了。
有没有一种方法可以编写一个锚定在Id上的定位器,并找到与之相关的点击按钮?感谢
<div class="ui-dialog-buttonset"><button type="button" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button" aria-disabled="false"><span class="ui-button-text">Ok</span></button></div>
您可以为这个元素尝试许多选择器。
//button[./span[@class='ui-button-text' and text()='Ok']]
//div[@class='ui-dialog-buttonset']/button
您可以尝试以下方法:
//button[@type='button']
DOM快照]
您要查找的元素可以通过XPath:简单地定位
"//div[@class='ui-dialog-buttonset']//button"
所以你的代码可能是这样的:
try:
digram_notfound = DRIVER_WAIT.until(EC.visibility_of_element_located((By.ID, "noresultsfound")))
if digram_notfound:
click_xpath(driver, "//div[@class='ui-dialog-buttonset']//button")
except TimeoutException:
#do useful stuff here