简体   繁体   English

无法使用 selenium 网络驱动程序和 Java 单击登录按钮

[英]Not able to click on Login button using selenium webdriver and Java

Hi I am trying to automate https://www.nextgenerationautomation.com and unable to click on login / SignUp button using Selenium 4嗨,我正在尝试自动化https://www.nextgenerationautomation.com并且无法使用 Selenium 4 来点击登录/注册按钮

Steps:脚步:

  1. Navigate to URL: https://www.nextgenerationautomation.com导航到 URL: https://www.nextgenerationautomation.com
  2. Click on LogIn/SignUp button.单击登录/注册按钮。

Issue: when I am using Thread.Sleep , code is working fine but when I am using explicit wait & implicit wait it's not working.问题:当我使用Thread.Sleep时,代码工作正常但是当我使用显式等待和隐式等待时它不起作用。
I have added Implicit in my base class at the time of driver initialization.我在驱动程序初始化时在我的基础 class 中添加了隐式。

Here is the code that I have tried.这是我试过的代码。

public class nextGenAutomationLoginPage extends Base {

    @FindBy(xpath = "(//button[@class='_1YW_5'])[1]")
    WebElement login;

    public nextGenAutomationLoginPage() {
        super();
        PageFactory.initElements(driver, this);
        // TODO Auto-generated constructor stub
    }

    public void clickOnLogin() throws InterruptedException {
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        System.out.println(driver.getTitle());
        // Thread.sleep(2000);
        wait.until(ExpectedConditions.elementToBeClickable(login));
        //wait.until(ExpectedConditions.presenceOfNestedElementLocatedBy(login, By.xpath("//div[@class='_10b1I']") ));
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("window.scrollBy(0,document.body.scrollHeight)");
        js.executeScript("arguments[0].scrollIntoView();", login);
        login.click();
        //driver.findElement(By.xpath("(//button[@class='_1YW_5'])[1]")).click();
        wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath("//div[@id='comp-kaoxkn4a1']")));
        String sign = driver.findElement(By.xpath("//div[@id='comp-kaoxkn4a1']/h1/span")).getText();
        System.out.println(sign);
    }

Note: I tried to add Scroll as well to add some wait before clicking.注意:我也尝试添加 Scroll 以在单击之前添加一些等待时间。

DOM: DOM: 在此处输入图像描述 Please let me know if i am missing anything here.如果我在这里遗漏任何东西,请告诉我。

Hi I got the point whats happening here:嗨,我知道这里发生了什么:

  1. Manually navigate to URL: https://www.nextgenerationautomation.com (Selenium also loading this URL)手动导航到 URL: https://www.nextgenerationautomation.com (Selenium 也加载此 URL)
  2. Manually Immediately keep clicking on "LogIn/SignUp button" (Selenium also able to click therefore NO error in console )手动 立即继续点击“登录/注册按钮”(Selenium 也可以点击因此控制台中没有错误)
  3. "LogIn/SignUp" page not opening unless enter image description here (LinkedIn following count widget ) gets appear on screen “登录/注册”页面不会打开,除非在此处输入图像描述(LinkedIn 关注计数小部件)出现在屏幕上
  4. Once enter image description here gets appear manually click on "LogIn/SignUp button", now Login page is opening (Selenium able to click after Thread.Sleep)一旦在这里输入图像描述,就会出现手动点击“登录/注册按钮”,现在登录页面正在打开(Selenium 能够在 Thread.Sleep 之后点击)

Summery:夏日:

  1. This is a codding defect on that page.这是该页面上的编码缺陷。
  2. "LogIn/SignUp" is not clickable until enter image description here gets added on page在页面上添加此处输入图片描述之前,“登录/注册”不可点击
  3. Your selenium code is perfectly fine:)您的 selenium 代码非常好:)
  4. Xpath I have used is //span[text()='Log In / SignUp'] Xpath 我用过的是//span[text()='Log In / SignUp']

Thanks KS感谢 KS

To click() on the element with text as Log In / SignUp you can use either of the following Locator Strategies :要在带有文本的元素上click() Log In / SignUp您可以使用以下任一定位器策略

  • xpath : xpath

     driver.findElement(By.xpath("//span[text()='Log In / SignUp']")).click();

However, as the element is a dynamic element, so to click() on the element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies :但是,由于元素是动态元素,因此要在元素上click() ,您需要为elementToBeClickable()引入WebDriverWait ,您可以使用以下任一定位器策略

  • xpath : xpath

     new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[text()='Log In / SignUp']"))).click();

Reference参考

You can find a detailed discussion on NoSuchElementException in:您可以在以下位置找到有关NoSuchElementException的详细讨论:

Actually your code is clicking the login button before the page is loaded properly.实际上,您的代码是在页面正确加载之前单击登录按钮。 please add visibility condition.请添加可见性条件。

wait.until(ExpectedConditions.visibilityOfElementLocated(by));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM