硒-不可点击的链接

时间:2019-08-29 13:19:16

标签: java selenium href

我正在使用Selenium库解析网站"https://www.diretta.it"。 当我一次又一次单击链接时,没有结果。

这是代码:

public static void main(String[] args)
{
    String url = "https://www.diretta.it/serie-a-2016-2017/";

    // System Property for Chrome Driver   
    System.setProperty("webdriver.chrome.driver","C:\\..\\chromedriver_win32\\chromedriver.exe");  

    // Instantiate a ChromeDriver class.      
    WebDriver driver=new ChromeDriver(); 
    driver.manage().window().maximize();

    driver.get(url);
    driver.findElement(By.xpath("//a[@href='/serie-a-2016-2017/risultati/']")).click();

    try 
    {
        Thread.sleep(3000); //  3 secondi
    } 
    catch (InterruptedException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

这部分代码就像没有执行

    WebElement element = driver.findElement(By.xpath("//a[@class='event__more event__more--static']"));
    Actions actions = new Actions(driver);
    actions.moveToElement(element).click().build().perform();       
}

这是我要单击的链接:

enter image description here

1 个答案:

答案 0 :(得分:1)

这是一个动态元素,因此您需要引起网络驱动程序等待:

WebDriverWait wait = new WebDriverWait(driver, 30);
String xpath = "//a[@class='event__more event__more--static']";
WebElement link = wait.until(ExpectedConditions.elementToBeClickable(By.xpath(xpath));

link.click();

与进口:

import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;