Java 无法使用Selenium NoTouchElementException定位元素(小部件)

Java 无法使用Selenium NoTouchElementException定位元素(小部件),java,selenium,Java,Selenium,我试图点击一个小部件按钮,它有扩展和折叠的功能。由于某些原因,我在定位此元素时遇到一些困难。我总是被抛出一个NoTouchElementException: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"cssselector","selector":"span[id^='d2l_1_24']>a[title='Expand News']"}

我试图点击一个小部件按钮,它有扩展和折叠的功能。由于某些原因,我在定位此元素时遇到一些困难。我总是被抛出一个NoTouchElementException:

org.openqa.selenium.NoSuchElementException:
no such element: Unable to locate element: {"method":"cssselector","selector":"span[id^='d2l_1_24']>a[title='Expand News']"}
有趣的是,当我使用FireFox firebug定位元素时,我对以下内容没有任何困难:

span[id^='d2l_1_24']>a[title='Expand News']
span[id^='d2l_1_24']>a[title='Collapse News']
news_widget_expand = driver.findElement(By.cssSelector("div.d2l-homepage-header-collapse-wrapper>span>a[title='Expand News']"));
news_widget_collapse = driver.findElement(By.cssSelector("div.d2l-homepage-header-collapse-wrapper>span>a[title='Collapse News']"));
它总是返回一个节点

在对前面的问题进行了一些调查之后,我排除了小部件位于单独的iFrame上的可能性。开发工具表明它位于顶部窗口

HTML格式如下:

<div id="d2l_1_169_906" class="d2l-widget-header d2l-widget-header-clickable d2l_1_166_935 d2l_1_167_808 d2l_1_168_85 d2l_1_165_766" data-d2l-collapsed="d2l_1_165_766" onclick="D2L.O("__g1",25)()">
<div class="d2l-homepage-header-wrapper">
<div class="d2l-homepage-header-menu-wrapper">
<div class="d2l-homepage-header-collapse-wrapper">
<span id="d2l_1_24_470" class="d2l-expandcollapse d2l_1_117_742">
<a id="d2l_1_186_505" class="d2l-imagelink d2l_1_117_742 d2l_1_187_114 vui-outline" href="javascript:void(0);" onclick="return false;" title="Expand News" aria-label="Expand News" role="button"/>
<a id="d2l_1_188_574" class="d2l-imagelink d2l_1_117_742 d2l_1_189_251 vui-outline d2l-hidden" href="javascript:void(0);" onclick="return false;" title="Collapse News" aria-label="Collapse News" role="button"/>
</span>
</div>
<div class="d2l-clear"/>
</div>
//instance fields
static WebDriver driver;
private WebElement news_widget_expand;
private WebElement news_widget_collapse;

//constructor
public HomePage(){

    WebDriver driver;

}   

public void set_pageobject_news_widget_dropdown(WebDriver driver){

    try {

        driver.switchTo().defaultContent();
        news_widget_collapse = driver.findElement(By.cssSelector("span[id^='d2l_1_24']>a[title='Expand News']"));
        //news_widget_expand = driver.findElement(By.xpath(".//*[starts-with(@id, 'd2l_1_186')]"));
        //news_widget_expand = driver.findElement(By.cssSelector("div[class='d2l-homepage']>div>div>div>div>span[id^='d2l_1_24']>a[title='Expand News']"));


        news_widget_collapse = driver.findElement(By.cssSelector("span[id^='d2l_1_24']>a[title='Collapse News']"));
        //news_widget_collapse = driver.findElement(By.xpath(".//*[starts-with(@id, 'd2l_1_188')]"));
        //news_widget_expand = driver.findElement(By.cssSelector("div[class='d2l-homepage']>div>div>div>div>span[id^='d2l_1_24']>a[title='Collapse News']"));

    } catch (Exception e) {
        System.out.println("Couldnt set page objects");
        e.printStackTrace();
    }

}

public void collapse_news(WebDriver driver) throws InterruptedException{

    try {
        news_widget_collapse.click();
        Thread.sleep(1000);
    } catch (Exception e) {

        e.printStackTrace();
        System.out.println("Couldn't collapse the news widget...");
    }
}

public void expand_news(WebDriver driver) throws InterruptedException{

    try {
        news_widget_expand.click();
        Thread.sleep(1000);
    } catch (Exception e) {

        e.printStackTrace();
        System.out.println("Couldn't expand the news widget...");
    }
Thread.sleep(2000);
//expand the widget
hp.set_pageobject_news_widget_dropdown(driver);
hp.expand_news(driver);
}

代码的调用如下所示:

<div id="d2l_1_169_906" class="d2l-widget-header d2l-widget-header-clickable d2l_1_166_935 d2l_1_167_808 d2l_1_168_85 d2l_1_165_766" data-d2l-collapsed="d2l_1_165_766" onclick="D2L.O("__g1",25)()">
<div class="d2l-homepage-header-wrapper">
<div class="d2l-homepage-header-menu-wrapper">
<div class="d2l-homepage-header-collapse-wrapper">
<span id="d2l_1_24_470" class="d2l-expandcollapse d2l_1_117_742">
<a id="d2l_1_186_505" class="d2l-imagelink d2l_1_117_742 d2l_1_187_114 vui-outline" href="javascript:void(0);" onclick="return false;" title="Expand News" aria-label="Expand News" role="button"/>
<a id="d2l_1_188_574" class="d2l-imagelink d2l_1_117_742 d2l_1_189_251 vui-outline d2l-hidden" href="javascript:void(0);" onclick="return false;" title="Collapse News" aria-label="Collapse News" role="button"/>
</span>
</div>
<div class="d2l-clear"/>
</div>
//instance fields
static WebDriver driver;
private WebElement news_widget_expand;
private WebElement news_widget_collapse;

//constructor
public HomePage(){

    WebDriver driver;

}   

public void set_pageobject_news_widget_dropdown(WebDriver driver){

    try {

        driver.switchTo().defaultContent();
        news_widget_collapse = driver.findElement(By.cssSelector("span[id^='d2l_1_24']>a[title='Expand News']"));
        //news_widget_expand = driver.findElement(By.xpath(".//*[starts-with(@id, 'd2l_1_186')]"));
        //news_widget_expand = driver.findElement(By.cssSelector("div[class='d2l-homepage']>div>div>div>div>span[id^='d2l_1_24']>a[title='Expand News']"));


        news_widget_collapse = driver.findElement(By.cssSelector("span[id^='d2l_1_24']>a[title='Collapse News']"));
        //news_widget_collapse = driver.findElement(By.xpath(".//*[starts-with(@id, 'd2l_1_188')]"));
        //news_widget_expand = driver.findElement(By.cssSelector("div[class='d2l-homepage']>div>div>div>div>span[id^='d2l_1_24']>a[title='Collapse News']"));

    } catch (Exception e) {
        System.out.println("Couldnt set page objects");
        e.printStackTrace();
    }

}

public void collapse_news(WebDriver driver) throws InterruptedException{

    try {
        news_widget_collapse.click();
        Thread.sleep(1000);
    } catch (Exception e) {

        e.printStackTrace();
        System.out.println("Couldn't collapse the news widget...");
    }
}

public void expand_news(WebDriver driver) throws InterruptedException{

    try {
        news_widget_expand.click();
        Thread.sleep(1000);
    } catch (Exception e) {

        e.printStackTrace();
        System.out.println("Couldn't expand the news widget...");
    }
Thread.sleep(2000);
//expand the widget
hp.set_pageobject_news_widget_dropdown(driver);
hp.expand_news(driver);

这里有什么明显的遗漏吗?

不确定这个答案是否对其他人有用,但我设法解决了它。我认为这个问题是由于一些使用css和html的经验不足造成的,但是如果我做以下操作,我就能够使上述代码正常工作:

span[id^='d2l_1_24']>a[title='Expand News']
span[id^='d2l_1_24']>a[title='Collapse News']
news_widget_expand = driver.findElement(By.cssSelector("div.d2l-homepage-header-collapse-wrapper>span>a[title='Expand News']"));
news_widget_collapse = driver.findElement(By.cssSelector("div.d2l-homepage-header-collapse-wrapper>span>a[title='Collapse News']"));
我还通过创建一个WebElement列表,获取所有具有指定cssSelector的WebElement,从而确认WebElements实际上在屏幕上可见:

enter List<WebElement> buttons = driver.findElements(By.cssSelector("div.d2l-homepage-header-collapse-wrapper>span>a"));
        int noButtons = buttons.size();
        System.out.println("Total webelements = "+noButtons);
        if(noButtons>0){
            for(WebElement b : buttons){

                if( b.getAttribute("title").contains("Expand News"))
                    b.click();
                if( b.getAttribute("title").contains("Collapse News"))
                    b.click();


            }
        }else
            System.out.println(b.getAttribute("title"));code here

我在这方面浪费了很多时间,尝试了让Webdriver点击元素的不同方法,比如xpath和不同的CSSSelector。我最终到达了那里,通过该WebElement的循环,我确信这些元素确实在那里。

根据您发布的HTML,扩展新闻是
id=“d2l\U 1\U 186\U 505”
@ScaryWombat该id不是静态的,每次页面刷新时它都会更改。我尝试了以下操作:
driver.findelelement(By.cssSelector(“div[id^='d2l_1_186_']][title='Expand News'])。单击()