我正在尝试使用 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();
最佳答案
要处理动态元素,请在预期条件下使用 WebDriverWait
和 elementToBeClickable
。使用以下 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/