ChromeDriver 不会点击元素。Selenium。爪哇岛



我正在制作场景以自动在此站点上自动化某些操作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的元素。我已经写了测试以获取具有此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();

to

  driver.findElement(By.xpath("//*[@id='content-login']")).click();

更新:

如果webdriver click()不起作用,请尝试使用JavascriptExecutor单击如下:

((JavascriptExecutor) driver).executeScript("arguments[0].click()",driver.findElement(By.xpath("//*[@id='content-login']")));

相关内容

  • 没有找到相关文章

最新更新