【问题标题】:selenium - not clickable link [duplicate]硒 - 不可点击的链接[重复]
【发布时间】:2020-01-02 18:53:57
【问题描述】:

我正在使用 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

【问题讨论】:

    标签: java selenium href


    【解决方案1】:

    这是一个动态元素,因此您需要诱导 Web 驱动程序等待:

    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;
    

    【讨论】:

    • 谢谢,但这个解决方案在我的情况下不起作用。
    猜你喜欢
    • 1970-01-01
    • 2014-09-20
    • 2019-12-26
    • 1970-01-01
    • 2011-05-25
    • 1970-01-01
    • 2021-04-02
    • 2013-01-29
    • 1970-01-01
    相关资源
    最近更新 更多