在http://yearup.org上有一个“立即申请”按钮。
<a href="https://www.yearup.org/seize-opportunity/" class="button">Apply Now</a>
到目前为止我试过:
//*[@class='button']") (but there are 11 occurrences. Using [0] doesn't help
//*[@class='button']//*[text()='Apply Now']
"html/body/div/section[2]/header/section/ul/li/ul/div[2]/a"
.findElement(By.linkText("Apply Now"));
他们都没有为我工作。
获得以下错误:
org.openqa.selenium.ElementNotInteractableException: Cannot click on element
是什么让它有点困难,是同一页面中相同标签的重复。有两种解决方案:
//div[@class='large-9 columns large-centered center-absolute']/a[@class='button' and contains(text(),'Apply Now')]
要么
(//a[contains(text(),'Apply Now')])[2]
这将有效:
driver.findElement(By.xpath(".//*[@href='http://www.yearup.org/seize-opportunity/']")).click();
如果您只是尝试找到它而不是单击该元素,请使用以下命令:
driver.findElement(By.xpath(".//*[@href='http://www.yearup.org/seize-opportunity/']"));
而且,如果您正试图前往该页面并且您不需要实际点击要在那里旅行的元素,那么只需执行以下操作:
driver.get("http://www.yearup.org/seize-opportunity/");
我知道在HTML中它显示为“立即应用”但是如果你使用的话
driver.findElement(By.linkText("APPLY NOW")).click();
有用。我刚试过它。