首页 > 解决方案 > 如何查找旁边带有特定文本的 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 而不是我的方法?(我认为是:))但我正在寻找意见)

标签: pythonselenium-webdriver

解决方案


可以有多种方法在此处编写相对于文本的 xpath。你可以试试

//div[text()='Login is required.']/preceding-sibling::div/input

OR

//div[text()='Login is required.']/parent::div//input[contains(@class,'TextBox')]

推荐阅读