Selenium (python) 定位按钮相对于另一个带有 Id 的 div
Selenium (python) locating button relative to another div with Id
我需要点击页面上的“确定”按钮(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
我能在页面中找到的唯一稳定定位器是 noresultsfound 包含子按钮的 div 之前的 div 的 ID div.
我猜不出我可以使用哪个 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 的定位器并找到相对于它的按钮来单击?谢谢
你可以试试这些:
//button[@type='button']
DOM Snapshot]
<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
您要找的元素可以通过这个 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
我需要点击页面上的“确定”按钮(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
我能在页面中找到的唯一稳定定位器是 noresultsfound 包含子按钮的 div 之前的 div 的 ID div.
我猜不出我可以使用哪个 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 的定位器并找到相对于它的按钮来单击?谢谢
你可以试试这些:
//button[@type='button']
DOM Snapshot]
<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
您要找的元素可以通过这个 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