我需要使用CSS或HTML的XPATH识别按钮的帮助



我正在使用带有Python的WebDriver自动化我们的网站。我很难识别和单击管理按钮。我试图切换到iframe并选择按钮。它没有找到按钮。
我已经与开发人员进行了交谈,开发人员说这不在iframe中。GUI是在GWT中完成的。
我只需要算出CSS或XPath,以便我可以单击按钮。
CSS会比XPath快得多。

我尝试了以下XPath://div [@class =" gwt-tablayoutpaneltabs"]/div [last()]

使用查找按钮在Selenium IDE中测试按钮时,它突出显示了该按钮的背景颜色。

有人知道CSS还是绝对XPath?

HTML如下(管理是底部的最后一个Div):

<html style="overflow: hidden;">
<head>
<body style="margin: 0px;">
<iframe id="__gwt_historyFrame" style="position: absolute; width: 0; height: 0; border: 0;" tabindex="-1" src="javascript:''">
<html>
<head>
</head>
<body>
</html>
</iframe>
<noscript> <div style="width: 22em; position: absolute; left: 50%; margin-left: -11em; color: red; background-color: white; border: 1px solid red; padding: 4px; font-family: sans-serif;"> Your web browser must have JavaScript enabled in order for this application to display correctly.</div> </noscript>
<script src="spinner.js" type="text/javascript">
<script type="text/javascript">
<script src="ClearCore/ClearCore.nocache.js" type="text/javascript">
<script defer="defer">
<iframe id="ClearCore" src="javascript:''" style="position: absolute; width: 0px; height: 0px; border: medium none;" tabindex="-1">
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script>
<script type="text/javascript">
<script type="text/javascript">
</head>
<body>
</html>
</iframe>
<div style="position: absolute; z-index: -32767; top: -20cm; width: 10cm; height: 10cm; visibility: hidden;" aria-hidden="true"> </div>
<div style="position: absolute; left: 0px; top: 0px; right: 0px; bottom: 0px;">
<div style="position: absolute; z-index: -32767; top: -20ex; width: 10em; height: 10ex; visibility: hidden;" aria-hidden="true"> </div>
<div style="position: absolute; overflow: hidden; left: 0px; top: 0px; right: 0px; bottom: 0px;">
<div style="position: absolute; left: 0px; top: 0px; right: 0px; bottom: 0px;">
<div style="position: absolute; z-index: -32767; top: -20ex; width: 10em; height: 10ex; visibility: hidden;" aria-hidden="true"> </div>
<div style="position: absolute; overflow: hidden; left: 1px; top: 1px; right: 1px; bottom: 1px;">
<div class="gwt-TabLayoutPanel" style="position: absolute; left: 0px; top: 0px; right: 0px; bottom: 0px;">
<div style="position: absolute; z-index: -32767; top: -20ex; width: 10em; height: 10ex; visibility: hidden;" aria-hidden="true"> </div>
<div style="position: absolute; overflow: hidden; left: 0px; top: 0px; right: 0px; height: 30px;">
<div class="gwt-TabLayoutPanelTabs" style="position: absolute; left: 0px; right: 0px; bottom: 0px; width: 16384px;">
<div class="gwt-TabLayoutPanelTab GEGQEWXCK gwt-TabLayoutPanelTab-selected" style="background-color: rgb(254, 255, 238);">
<div class="gwt-TabLayoutPanelTab GEGQEWXCK" style="background-color: rgb(254, 255, 238);">
<div class="gwt-TabLayoutPanelTab GEGQEWXCK" style="background-color: rgb(254, 255, 238);">
<div class="gwt-TabLayoutPanelTab GEGQEWXCK" style="background-color: rgb(254, 255, 238);">
<div class="gwt-TabLayoutPanelTab GEGQEWXCK" style="background-color: rgb(254, 255, 238);">
<div class="gwt-TabLayoutPanelTab GEGQEWXCK" style="background-color: rgb(254, 255, 238);">
<div class="gwt-TabLayoutPanelTab GEGQEWXCK" style="background-color: rgb(254, 255, 238);">
<div class="gwt-TabLayoutPanelTabInner">
<div class="gwt-HTML">Administration</div>
</div>
</div>   

我的管理按钮代码:

adminButton = mydriver.find_element_by_xpath('//div[@class="gwt- TabLayoutPanelTabs"]/div[last()]')
adminButton.click()

我也尝试了:

mydriver.find_element_by_xpath('//div[7]/div/div').click()

您需要找到嵌套的iframe s,然后在找到内部的元素之前,一个一个一个切换到它们:

driver.switch_to.frame("__gwt_historyFrame")
driver.switch_to.frame("ClearCore")
administration = driver.find_element_by_xpath("//div[. = 'Administration']")
administration.click()

请注意,我正在通过文本找到该元素,我认为这更"自然"和逻辑。

如果"管理"链接不在iframe内部,请省略两个" switch_to"行。

您可能还需要等待该元素变得可单击:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
administration = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.XPATH, "//div[. = 'Administration']"))
)
administration.click()

我终于设法解决了我的问题。开发人员说,我必须等待页面完全完成的加载。当所有元素显示在屏幕上时,该页面仍在加载JavaScript函数。
我首先尝试了时间。Sleep(30),然后单击按钮。有效。每次等待30秒不是有效的。然后,我使用了WebDriverWait,这更有效。这是我使用的代码:

WebDriverWait(mydriver, 10).until(lambda d: mydriver.find_element_by_xpath("//div[. = 'Administration']").click())

最新更新