看似简单的div类元素但是selenium cant click

时间:2017-10-03 19:16:31

标签: java eclipse selenium selenium-webdriver webdriver

似乎是简单的div类元素,但是selenium cant click

以下是代码

中的凭据

离线-wealthytarundas2015@gmail.com

PWD:-Tapan @ 321

            package basic.basic;

            import java.util.concurrent.TimeUnit;

            import org.openqa.selenium.By;
            import org.openqa.selenium.Keys;
            import org.openqa.selenium.WebDriver;
            import org.openqa.selenium.chrome.ChromeDriver;

            public class Add {

            public static void main(String[] args) throws InterruptedException {
            // 

            System.setProperty("webdriver.chrome.driver","C://Sprints//chromedriver_win32//chromedriver.exe");
                     WebDriver driver = new ChromeDriver();
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

                 driver.get("http://addmefast.com//");

                 driver.findElement(By.name("email")).sendKeys("wealthytarundas2015@gmail.com");

                 driver.findElement(By.name("password")).sendKeys("Tapan@321");

                 driver.findElement(By.name("login_button")).click();
                 Thread.sleep(4000);   

                 driver.findElement(By.linkText("YouTube Likes")).sendKeys(Keys.RETURN);
                 Thread.sleep(6000);
                 driver.findElement(By.className("btn3")).sendKeys(Keys.RETURN); //CODE TO CLICK LINK buttton but getting error here



            }

            }

eclipse控制台出错: -

Starting ChromeDriver 2.32.498550 (9dec58e66c31bcc53a9ce3c7226f0c1c5810906a) on port 10411
Only local connections are allowed.
Oct 03, 2017 11:55:38 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: cannot focus element
  (Session info: chrome=61.0.3163.100)
  (Driver info: chromedriver=2.32.498550 (9dec58e66c31bcc53a9ce3c7226f0c1c5810906a),platform=Windows NT 10.0.15063 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds

3 个答案:

答案 0 :(得分:0)

我在代码中引入了显式等待(用于显示在页面中的“链接”按钮),然后我找到了“喜欢”按钮并单击它。

尝试下面的代码,让我知道它是否适合您。

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Add {
    public static void main(String[] args) throws InterruptedException {
      System.setProperty("webdriver.chrome.driver","C://Sprints//chromedriver_win32//chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
      driver.get("http://addmefast.com//");
      driver. manage(). window().maximize();
    driver.findElement(By.name("email")).sendKeys("wealthytarundas2015@gmail.com");
      driver.findElement(By.name("password")).sendKeys("Tapan@321");
      driver.findElement(By.name("login_button")).click();
      Thread.sleep(4000);
      driver.findElement(By.linkText("YouTube Likes")).sendKeys(Keys.RETURN);
      Thread.sleep(6000);
      WebDriverWait wait=new WebDriverWait(driver, 20);
      WebElement like;
      like= wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("btn3")));
        like.click();
    }
}

干杯!!

答案 1 :(得分:-1)

我认为你无法点击div" btn3"因为它在链接内部。

driver.findElement(By.className("btn3")).sendKeys(Keys.RETURN); 

请尝试以下操作:

更新:

driver.findElement(By.CssSelector("div#likedPagesSingle a"));

答案 2 :(得分:-1)

让它正常运行,感谢回复((JavascriptExecutor)driver).executeScript("arguments[0].click()", driver.findElement(By.xpath("//a[@class = 'single_like_button btn3-wrap']/div[@class = 'btn3']")));