我正在使用Webdriver with Python自动化我们的网站。我无法识别并单击“管理”按钮。我试图切换到iFrame并选择按钮。它没有找到按钮 我已经与开发人员交谈了,开发人员说它不在iFrame中。 GUI在GWT中完成 我只需要编写CSS或XPath,这样我就可以点击按钮了 CSS会更好,因为它比XPath更快。
我尝试过以下XPath: // DIV [@class =" GWT-TabLayoutPanelTabs"] /格[最后()]
使用“查找”按钮在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()
答案 0 :(得分:4)
在找到内部元素之前,您需要逐个找到nested iframe
s和switch to them :
driver.switch_to.frame("__gwt_historyFrame")
driver.switch_to.frame("ClearCore")
administration = driver.find_element_by_xpath("//div[. = 'Administration']")
administration.click()
请注意,我正在按文字查找元素,我认为这更“自然”且合乎逻辑。
如果“管理”链接不在iframe
内,请省略两个“switch_to”行。
您可能还需要wait for the element to become clickable:
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()
答案 1 :(得分:2)
我终于设法解决了我的问题。开发人员说我必须等待页面完全完成加载。当所有元素都显示在屏幕上时,页面仍在加载JavaScript函数 我首先尝试time.sleep(30)然后单击按钮。有效。每次等待30秒效率不高。然后我使用了WebDriverWait,这样效率更高。 这是我使用的代码:
WebDriverWait(mydriver, 10).until(lambda d: mydriver.find_element_by_xpath("//div[. = 'Administration']").click())