java - 尝试单击包含空格的超链接

标签 java selenium hyperlink

我正在尝试使用 Java 和 Selenium 单击带有空格的超链接。这是代码示例

<h3 class="side menu">
    <a class="side-menu" href="/configurations">
    <span class="menu-icon ca"></span>Configuration
    </a>
</h3>

我尝试过使用 xpath 开头和包含,但没有成功。

driver.findElement(By.xpath("//a[starts-with(text(),’Configuration’)]")).click();

最佳答案

要处理动态元素,请在预期条件下使用 WebDriverWaitelementToBeClickable。使用以下 Xpath 定位器策略。

WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath('//a[@class="side-menu"][normalize-space(.)="Configuration"]')));
element.click()

或者

WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath('//a[@class="side-menu"][contains(.,"Configuration")]')));
element.click()

关于java - 尝试单击包含空格的超链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55959934/

相关文章:

java - 好奇的 if 语句行为

html - 防止 html 表单字段中的链接

jQuery:动画文本颜色

java - 将文件作为链接而不是邮件中的附件发送

java - selenium 等待代理页面加载超时

java - MediaPlayer从毫秒转换为分钟和秒

java - 自定义字体为 Android 中的某些字母组合提供了不正确的字形

java - 无法使用 Selenium Webdriver 定位元素

java - Eclipse 中的控制台输出颜色

python - 循环中的 Selenium-chrome driver.get() 重复几次后会中断