没有这样的元素:无法在硒中找到元素

时间:2018-11-28 09:32:55

标签: java selenium

我正在尝试打开youtube链接,然后单击频道,但可惜它找不到频道链接。以下链接显示带有红色箭头的目标链接。

enter image description here

因此,这是我的代码,有关更多详细信息,您可以检查此repo

    Driver driver = new Driver();
    driver.laodPage("https://www.youtube.com/watch?v=tFPzE4Twj4k");
    driver.getCurrentURL();
  //  driver.getElementById("img").click(); 
    driver.click("//*[@id=\"img\"]"); //Click on video channel 
    driver.getCurrentURL();

但它抱怨:

NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="img"]"}

我在chrome控制台上检查了xpath,它给了我21个元素的数组,所以我想也许是因为它。然后,我尝试将其更改为"//*[@id=\"img\"][1]",它给了我一个元素,但仍然有问题。

1 个答案:

答案 0 :(得分:0)

点击ID“头像”即可:

<ListBox ItemsSource="{Binding WeeklyWeather}"
         SelectedItem="{Binding SelectedDailyWeather, Mode=TwoWay}">

    //Use ItemTemplate to set how item looks "inside"
    //I'll leave design details for you
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <TextBlock Text="{Binding Day}"/>
                <Image Source={Binding WheatherPicturePath}/>
                <TextBlock Text="{Binding Temperature}"/>
                <TextBlock Text="{Binding Description}"/>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>

    //ItemsPanel defines container for items. It can be StackPanel, Wrapanel, Grid, etc
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel IsItemsHost="True" Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>

    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListBoxItem}">
                        //You use this place to design how container normally looks like
                        <Border Background="White">
                            //DataTemplate defined above is placed in ContentPresenter
                            <ContentPresenter />
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                //Here we catch "IsSelected" event and re-design our template to visually reflect "selected"
                <Trigger Property="IsSelected" Value="true">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type ListBoxItem}">
                                <Border Background="Gray">
                                    <ContentPresenter />
                                </Border>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Trigger>
            </Style.Triggers>
        </Style>
    </ListBox.ItemContainerStyle>

</ListBox>