java - 无法根据以下锚标记定位元素
问题描述
请告知如何在以下代码中找到新业务标签的链接。我已经尝试了以下 xpath,但它没有工作:
driver.findElement(By.linkText("NEW BUSINESS")).click
driver.findElement(By.xpath("//span[@class='hdBottomBar']/a[1]"))
HTML:
<span class="hdBottomBar">
<a class="hdTopBar" href="javascript: void navCntl('NewBusiness','NavBar');" onmouseover="window.status='New Business';return true" onmouseout="window.status='';return true" name="newBusiness">NEW BUSINESS</a>
解决方案
该元素是启用了JavaScript的元素,因此要调用click()
您必须诱导WebDriverWait以使该元素可点击,并且您可以使用以下任一解决方案:
linkText
:new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.linkText("NEW BUSINESS"))).click();
cssSelector
:new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("span.hdBottomBar>a.hdTopBar[name='newBusiness']"))).click();
xpath
:new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[@class='hdBottomBar']/a[@class='hdTopBar' and @name='newBusiness'][text()='NEW BUSINESS']"))).click();
推荐阅读
- spring-boot - 如何在 Kong 后面配置 Spring OpenAPI UI
- python - Openpyxl 和二分搜索
- gitlab - SonarQube OAuth Gitlab 重定向故障
- aframe - GLTF 不渲染,卡在蓝色加载屏幕
- excel - 如何根据计数器变量的值在命名范围内引用特定的、不断变化的行?
- google-cloud-kms - 使用旋转销毁密钥
- vue.js - 将 Fetch 函数的结果传递给 Parent Prop
- blazor - 升级到 .net 6 时托管的 Blazor WASM 身份验证中断
- azure - 无法将设备添加到 Azure Active Directory,因为它有两个对象 ID
- python - 如何在不执行父类中的dunder方法的情况下继承父类?