python - 如何查找旁边带有特定文本的 GWT 元素
问题描述
因此,我尝试改进代码和 XPATH,并尝试像这样处理字段:
<div class="h3">Login</div> <div class="Is3WC-spot Is3WC-spotInfo icon xdm icon_information_16 dialogInfoSpot" style="display: none;" aria-hidden="true"><div class="Is3WC-spotRelativeDivPos"> <div class="Is3WC-spotTextMargins">User Name (login) must:<br>- contain at least 3 characters<br>- be shorter than 60 characters</div> <div class="Is3WC-spotClose icon hover xdm icon_delete_minor_white"></div> </div></div> <div class="floatLeft"><div><input type="text" class="dialogTextBox error"></div> <div class="Is3WC-errorLabel">Login is required.</div></div> <div class="dialogInformationIcon iconInformation clickable"></div>
如何更好地创建该 xpath? 登录单元
现在它的工作原理是:"/html/body/div[8]/div/table/tbody/tr[2]/td[2]/div/div/div/div[2]/div[3]/div[1]/input"
而且我一点也不为此感到自豪......
还有一个关于 WebDriverWait 的问题。在主题How to click on GWT enabled elements using Selenium and Python一个人推荐我使用 WebDriverWait 但我有自己的方法:
@staticmethod
def clickPerform(driver, xpath):
counter = 0;
while True:
try:
driver.find_element_by_xpath(xpath).click()
break
except ElementClickInterceptedException:
time.sleep(1)
if counter == 10:
Verify.verifyAssert(True, False,"Not able to click ElementClickInterceptedException: " + xpath)
break
counter = counter + 1
except ElementNotInteractableException:
time.sleep(1)
if counter == 10:
Verify.verifyAssert(True, False,"Not able to click ElementNotInteractableException: " + xpath)
break
counter = counter + 1
最好使用提到的 WebDriverWait 而不是我的方法?(我认为是:))但我正在寻找意见)
解决方案
可以有多种方法在此处编写相对于文本的 xpath。你可以试试
//div[text()='Login is required.']/preceding-sibling::div/input
OR
//div[text()='Login is required.']/parent::div//input[contains(@class,'TextBox')]
推荐阅读
- r - 如何使用 gtsummary 库以不同方式构建描述性统计表
- r - 当您需要超过 2 个结果时,如何根据现有字符列改变新列?
- c# - 以编程方式浏览 365 SharePoint 网站 Microsoft.Graph.GraphServiceClient
- pdf - 具有托管在网络服务器上的命名目的地的 PDF 无法通过 URL 锚导航到某些目的地
- javascript - 如何以角度清理 html 以避免 XSS 漏洞
- c# - 从 TreeView C# 将变量分配给二维数组
- node.js - 使用 res.redirect() 将标头发送到客户端后无法设置标头
- html - 在内联样式中使用流体变量
- c# - 设置 showdialog 设置的图标
- javascript - 在初始化 firebaseApp 时,将区域设置为 europe-west1,而不是 us-central1