[英]ChromeDriver doesn't click on element. Selenium. Java
我正在制作使該網站上的某些操作自動化的方案https://csgo500.com/
我班上的代碼:
package scenario;
import managers.loaders.CheckBy;
import driver.sleep.DriverSleeper;
import exceptions.NotNowException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
public class CSGO500Scen implements SiteScenarioInt{
private WebDriver driver;
public CSGO500Scen(WebDriver driver){
this.driver = driver;
}
public void gamble() throws NotNowException {
driver.get("https://csgo500.com/");
CheckBy.id("gotit-btn");
driver.findElement(By.id("gotit-btn")).click(); //accept terms of use
DriverSleeper.sleep(3);
CheckBy.id("content-login");
DriverSleeper.sleep(3);
driver.findElement(By.xpath("//*[@id=\"content-login\"]")).click(); //HERE IS A PROBLEM
CheckBy.className("btn_green_white_innerfade");
driver.findElement(By.className("btn_green_white_innerfade")).click(); //login with steam
CheckBy.className("nav-rewards");
driver.findElement(By.className("nav-rewards")).click();
if(!isActive()){
throw new NotNowException("CSGO500.com");
}
else{
while(true){
DriverSleeper.sleep(3);
if (!isActive()){
break;
}
}
}
}
private boolean isActive(){
if (driver.findElement(By.id("reward-claim-submit-disabled")).getAttribute("style").equals("display: none;")){
return true;
}
else{
return false;
}
}
}
第一步是接受使用條款。 好的,完成了,但是當我想單擊“登錄”按鈕時,出現以下錯誤(登錄按鈕ID為“ content-login”):
Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: Element <a id="content-login" href="https://www.csgo-xchange.com/steam" data-lang="login" class="" data-__original="
...
" data-__trid="1000018" data-__translated="en">Login</a> is not clickable at point (946, 33). Other element would receive the click: <div id="login-content">...</div>
它寫道,該頁面還包含一個具有相同ID的元素。 我已經編寫了Test以獲取具有此ID的元素數量:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
public class test {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\user\\IdeaProjects\\sitescen\\src\\main\\resources\\chromedriver.exe" );
WebDriver driver = new ChromeDriver();
driver.get("https://www.csgo500.com/");
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
System.out.print(driver.findElements(By.xpath("//*[@id=\"content-login\"]")).size());
}
}
我得到了輸出:
1
因此,我只有一個具有相同ID的元素,並且沒有元素環繞此按鈕。
為了防止加載錯誤,我使用DriverSleeper
類的sleep
來接受睡眠秒數。
最后,我不知道如何單擊此按鈕,希望您對我有所幫助。
UPD:我在其中調用賭博方法的類:
public class Coleso {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\user\\IdeaProjects\\sitescen\\src\\main\\resources\\chromedriver.exe" );
WebDriver driver = new ChromeDriver();
DriverSleeper.setDriver(driver);
CheckBy.setDriver(driver);
SteamLogin log = new SteamLogin(driver);
CSGO500Scen site = new CSGO500Scen(driver);
try {
site.gamble();
} catch (NotNowException e) {
e.printStackTrace();
}
}
}
您的xpath有問題。 您需要更改此driver.findElement(By.xpath("//*[@id=\\"content-login\\"]")).click();
至
driver.findElement(By.xpath("//*[@id='content-login']")).click();
更新:
如果webdriver click()
無法正常工作,請嘗試使用JavascriptExecutor
單擊,如下所示:
((JavascriptExecutor) driver).executeScript("arguments[0].click()",driver.findElement(By.xpath("//*[@id='content-login']")));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.