【问题标题】:I need some help in identifying button using CSS or Xpath from the HTML在使用 HTML 中的 CSS 或 Xpath 识别按钮时,我需要一些帮助
【发布时间】:2015-05-06 14:59:25
【问题描述】:

我正在使用带有 Python 的 Webdriver 自动化我们的网站。我无法识别和单击“管理”按钮。我试图切换到 iFrame 并选择按钮。它没有找到按钮。
我已经和开发人员谈过了,开发人员说它不在 iFrame 中。 GUI 在 GWT 中完成。
我只需要计算出 CSS 或 XPath,以便我可以单击按钮。
CSS 会更好,因为它比 XPath 更快。

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

当我在 Selenium IDE 中使用 Find 按钮对其进行测试时,它会突出显示按钮的背景颜色。

请问有人知道 CSS 或绝对 XPath 吗?

HTML如下(Administration是底部的最后一个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()

【问题讨论】:

    标签: python css xpath selenium-webdriver webdriver


    【解决方案1】:

    您需要一一定位nested iframesswitch 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()
    

    【讨论】:

    • 感谢您的建议。我收到错误无法找到框架:ClearCore。如果我注释掉帧“_gwt_historyFrame”的第一行运行代码,我会收到以下错误:无法找到元素:{“method”:“xpath”,“selector”:“//div [。='Administration' ]"}
    • @RiazLadhani 您确定提供的标记实际上是正确的吗?该网站是公开的,您可以分享它的链接吗?如果没有,至少添加关闭 iframe 标记。谢谢。
    • 该 URL 不是公共 URL。该网站托管在 Tomcat 的本地计算机上。例如。 URL:vmtest01:8080/clearcore iFrame html 我看不到封闭的 iFrame 标记。
    • Administration div所在的html如下:
      Administration
    【解决方案2】:

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

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

    【讨论】:

    • 这不是我发布的答案吗?请参阅WebDriverWait 相关部分。
    • 啊,你有,好地方。对不起,我错过了那部分。我正在阅读太多帖子以找到解决方案。我会将您的答案标记为正确。谢谢!
    • 谢谢你,希望你已经彻底解决了你的问题。
    猜你喜欢
    • 2016-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多