[英]How to click button using selenium webdriver java, whose xpath is not constant
[英]How to fix "Method Call Expected" using java and selenium when using XPATH to click a button?
我对 selenium 的所有事情都很陌生,我现在很难弄清楚如何使用 XPATH 单击按钮(技术上是链接)来搜索正确的按钮。
我的进口是这些:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
我的代码在这里
System.setProperty("webdriver.chrome.driver", "C:\\selenium-java-2.35.0\\chromedriver_win32_2.2\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://LINK_OF_THE_WEBSITE");
WebElement button = WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.ByXPath("//a[@class='btn' @href'LINK_OF_THE_BUTTON']"))).click();
driver.close();
目前我的 IDE 中显示了 2 个错误,都告诉我“预期方法调用”。 其中一个在 WebDriverWait(driver, 10) 上,另一个在 By.ByXPath("...") 上
我已经查看了这两个对象的 JavaDocs,我觉得我正确地调用了它们,但我仍然遇到这些错误。
我不确定我使用的方法是否是 go 的最佳方法。 该按钮没有 ID,只有一个 class 和一个 href 值,它是一个带有文本“Sign In”的登录按钮。 任何帮助表示赞赏!
要单击link/button
,您需要使用正确的定位器识别元素。
使用下方xpath
//*[@class='btn' and text()='Sign In']
理想情况下你的代码会像
WebElement button=new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@class='btn' and text()='Sign In']")));
button.click();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.