我想從用戶輸入的按鍵(發送鍵)我應該如何實現呢?獲取用戶輸入的硒webderiver
driver.findElement(By.xpath(".//*[@id='vehicleNum']")).sendKeys("1121");
我想從用戶輸入的按鍵(發送鍵)我應該如何實現呢?獲取用戶輸入的硒webderiver
driver.findElement(By.xpath(".//*[@id='vehicleNum']")).sendKeys("1121");
作爲演示,我爲您提供的代碼樣本塊與對於URL https://accounts.google.com
這將要求來自用戶的輸入提供Email or Phone
,然後點擊按鈕Next
:
使用
Scanner
您將在IDE控制檯上獲得用戶提示,如Enter your Email or Phone :
。千萬記得要通過scanner_user.close();
明確關閉掃描儀,以防止Resource Leakage
。
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class GMAIL_LOGIN_SCANNER
{
public static void main(String[] args)
{
Scanner scanner_user, scanner_pass;
System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.addArguments("disable-infobars");
options.addArguments("--disable-extensions");
WebDriver driver = new ChromeDriver(options);
driver.get("https://accounts.google.com");
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
scanner_user = new Scanner(System.in);
System.out.println("Enter your Email or Phone : ");
String user = scanner_user.nextLine();
driver.findElement(By.xpath("//input[@id='identifierId']")).sendKeys(user);
driver.findElement(By.id("identifierNext")).click();
scanner_user.close();
}
}
thz其工作 – GHOST
您可以使用內置Scanner類從系統控制檯獲取輸入。下面的代碼可能會給你一些想法。
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
//now pass name in sendkeys.
driver.findElement(By.xpath(".//*[@id='vehicleNum']")).sendKeys(name);
不然,如果你需要從webelement的一個獲取文本,確定webelement和使用gettext()來獲取字符串值。
String value = driver.findElement(by.xpath("//")).getText();
driver.findElement(By.xpath(".//*[@id='vehicleNum']")).sendKeys(value);
希望這會有所幫助。謝謝..
即使你的問題並不清楚正確,但因爲我假設你需要下面的解決方案。 爲了從文本框中輸入值,可以使用下面的代碼:
driver.findElement(By.xpath(".//*[@id='vehicleNum']")).sendKeys("1121");
String str = driver.findElement(By.xpath(".//*[@id='vehicleNum']")).getAttribute("value");
System.out.println(str);
程序輸出將是「1121」
你想獲得您發送郵件使用的SendKeys(值 「1121」)? –