如何使用 Java 在 Selenium 中单击注册按钮,我能打开页面,但无法单击?
我们可以使用 Java 在 Selenium 中单击注册按钮。首先,我们必须使用 id、类名、名称、链接文本、xpath、css 或部分链接文本等任意定位器来识别该注册按钮。在识别之后,我们必须使用 click 方法来单击注册按钮。
语法
WebElement m=driver. findElement(By.id("loc-txt"));
m.click();示例
使用 click 代码实现
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.util.concurrent.TimeUnit;
public class SignIn{
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver",
"C:\Users\ghs6kor\Desktop\Java\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
//implicit wait
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
//URL launch
driver.get("https://www.linkedin.com/");
// identify element with class name then use click method
WebElement m=driver.
findElement(By.className("sign-in-form__submit-button"));
m.click();
driver.close();
}
}此外,我们还可以使用 sendKeys 方法单击注册按钮,并传递 Keys.ENTER 作为该方法的参数。
语法
WebElement m=driver. findElement(By.id("loc-txt"));
m.sendKeys(Keys.ENTER);示例
使用 sendKeys 代码实现
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Keys;
public class SignInSendKeys{
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver",
"C:\Users\ghs6kor\Desktop\Java\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
//implicit wait
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
//URL launch
driver.get("https://www.linkedin.com/");
// identify element with class name then use sendKeys method
WebElement m=driver.
findElement(By.className("sign-in-form__submit-button"));
m.sendKeys(Keys.ENTER);
driver.close();
}
}
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
安卓
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP