python - 我需要一些帮助来使用 HTML 中的 CSS 或 Xpath 识别按钮

标签 python css xpath selenium-webdriver webdriver

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

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

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

有人知道 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()

最佳答案

您需要找到 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()

关于python - 我需要一些帮助来使用 HTML 中的 CSS 或 Xpath 识别按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30080459/

相关文章:

python - 有没有办法自动从 mongoengine 中存储的 ListField 中删除列表元数据?

html - 调整窗口大小时从左下角制作背景图像 "grow"

xml - 如何在 Silverlight 中计算 XPath 表达式?

Python-在模板中查找第一个字典键

javascript - 无法将 javascript 数据传递给 python

javascript - Grails 3 - 资源插件

javascript - Bootstrap - 如何从元素中清除 css 以重新开始?

xml - XPath查询不匹配任何值

Xpath 获取没有完整空子节点的节点

python - Zip 密码功能工作错误