请在以下代码中建议如何找到“新业务”标签的链接。我尝试了以下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>
答案 0 :(得分:0)
尝试:
//span[@class='hdBottomBar']/a[@name='newBusiness']
或
//span[@class='hdBottomBar']/a[text()='NEW BUSINESS']
答案 1 :(得分:0)
该元素是启用了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();