gpt4 book ai didi

java - 如何通过 Selenium-WebDriver 正确查找元素以用于输入目的

转载 作者:行者123 更新时间:2023-12-02 05:32:14 25 4
gpt4 key购买 nike

我正在尝试创建一个程序来自动执行某些下载,但是,当使用 Selenium-WebDriver 时,我发现我似乎找不到登录所需的元素。我已经找到了正确的元素,但实际上使用WebDriver#findElement() 给我带来了问题。

<input id="form-username" class="form-field" form="popup-login" type="text" name="username" value="" tabindex="1" autofocus="">

我一直在尝试不同的By方法,但是它们都不起作用,还有不同的ID,尽管没有效果。

我检查了其他帖子,但它们似乎都不适合,因为它们只是从 HTML 中的特定点(如字符串)检索信息,我想在其中输入信息。

public void start(String usernameInfo, String passwordInfo) {
driver = new HtmlUnitDriver();
driver.get("https://www.nexusmods.com");

WebElement username = driver.findElement(By.id("form-username"));
username.sendKeys(usernameInfo);
username.submit();

WebElement password = driver.findElement(By.id("form-password"));
password.sendKeys(passwordInfo);
password.submit();

System.out.println(driver.getTitle());
driver.quit();
}

可以在此处查看输出日志:https://hastebin.com/zuvebosaha.nginx

更新:尝试了ChromeDriver,发现以下代码(修改为我使用)

public void start(String usernameInfo, String passwordInfo) {
System.setProperty("webdriver.chrome.driver","C:\\Users\\veeay\\Documents\\chromedriver.exe"); //add chrome driver path (System.setProperty("webdriver.chrome.drive",chrome driver path which you downloaded)

WebDriver driver = new ChromeDriver(); // create object of ChromeDriver

driver.manage().window().maximize(); // maximize the browser window

driver.get("https://www.nexusmods.com/"); //enter url

driver.findElement(By.id("form-username")).sendKeys(usernameInfo); //type textbox's id or name or any locater along with data in sendkeys

driver.findElement(By.id("form-password")).sendKeys(passwordInfo);

driver.findElement(By.id("btnLogin")).click();

try {
Thread.sleep(2000); //used thread for hold process
} catch (InterruptedException e) {
e.printStackTrace();
}

driver.quit(); //for close browser
}

结果如下:https://hastebin.com/iliyuvucok.cs

更新2:奇怪的是,现在我实际上发布了这个问题,我做得很好。现在我可以做所有事情,除了选择登录按钮。

public void start(String usernameInfo, String passwordInfo) {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\veeay\\Documents\\chromedriver.exe"); //add chrome driver path (System.setProperty("webdriver.chrome.drive",chrome driver path which you downloaded)

WebDriver driver = new ChromeDriver(); // create object of ChromeDriver

driver.manage().window().maximize(); // maximize the browser window

driver.get("https://www.nexusmods.com/Core/Libs/Common/Widgets/LoginPopUp?url=%2F%2Fwww.nexusmods.com%2F"); //enter url

driver.findElement(By.id("form-username")).sendKeys(usernameInfo); //type textbox's id or name or any locater along with data in sendkeys

driver.findElement(By.id("form-password")).sendKeys(passwordInfo);

driver.findElement(By.id("sign-in-button")).click();

try {
Thread.sleep(2000); //used thread for hold process
} catch (InterruptedException e) {
e.printStackTrace();
}

driver.quit(); //for close browser
}

显然登录按钮不可交互 https://hastebin.com/ahuvezoxat.cs

最佳答案

添加了显式等待并且它有效:

package vee;

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Vee {

@Test
public void start() {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\pburgr\\Desktop\\selenium-tests\\GCH_driver\\chromedriver.exe");

WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();

// new explicit wait
WebDriverWait webDriverWait = new WebDriverWait(driver, 5);

driver.get("https://www.nexusmods.com/Core/Libs/Common/Widgets/LoginPopUp?url=%2F%2Fwww.nexusmods.com%2F");

// using explicit wait
webDriverWait.until(ExpectedConditions.elementToBeClickable(By.id("sign-in-button")));

driver.findElement(By.id("form-username")).sendKeys("some name");
driver.findElement(By.id("form-password")).sendKeys("some password");

// print true or false by the button state
System.out.println(driver.findElement(By.id("sign-in-button")).isEnabled());

driver.findElement(By.id("sign-in-button")).click();

driver.quit();
}
}

输出:

Starting ChromeDriver 74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29}) on port 4301
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
[1560240089.419][WARNING]: This version of ChromeDriver has not been tested with Chrome version 75.
Čer 11, 2019 10:01:31 DOP. org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
true

也许在重复测试时,会弹出验证码并禁用该按钮。

关于java - 如何通过 Selenium-WebDriver 正确查找元素以用于输入目的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56194886/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com