如何点击href标签?

奥托·拉恩

我有 DOM

<div class="" data-zone-data="{"id":"97017425"}"data-zone-name="category-link">
<div class="_35SYu _1vnug" data-tid="acbe4a11 f8542ee1">
<a href="/catalog--kompiuternaia-tekhnika/54425" class="_3Lwc_">
<span class="_3z8Gf">Компьютеры</span>
</a>
</div>
</div>
enter code here

构造函数:

   public YandexBeforeSearch(WebDriver driver, String search){
        this.driver = driver;
        this.driver.get("https://yandex.ru/search?text=" + search);

    }

我的方法:

    public void clickToSite(){
        LinkYandexMarket = driver.findElement(By.xpath("//a[@accesskey='1']"));
        LinkYandexMarket.click();
    }
    public void clickTYandexPC(){
        LinkYandexPC = driver.findElement(By.xpath("//a[@href='/catalog--kompiuternaia-tekhnika/54425']"));
        LinkYandexPC.click();
    }

我的测试:

@Test
public void testOpen(){
    YandexBeforeSearch yandexBeforeSearch = new YandexBeforeSearch(chromeDriver, "yandex market");
    yandexBeforeSearch.clickToSite();
    yandexBeforeSearch.clickTYandexPC();
}

例外:

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//a[@href='catalog--kompiuternaia-tekhnika/54425']"}

我还使用了其他属性。该问题仅发生在此站点上。我无法点击该类别的链接。

帕马杜

//a[@accesskey='1']单击第一个链接时会打开一个新窗口。

我们需要切换到那个新窗口来点击 //a[@href='/catalog--kompiuternaia-tekhnika/54425']

// Click on the 1st link.
driver.findElement(By.xpath("//a[@accesskey='1']")).click();

// Get parent window.
String parentwindow = driver.getWindowHandle();

// Get all window handles.
Set<String> windows = driver.getWindowHandles();
List<String> windows1 = new ArrayList<>(windows);

// Switch to the new window to click on the other link.
driver.switchTo().window(windows1.get(1));
driver.findElement(By.xpath("//a[@href='/catalog--kompiuternaia-tekhnika/54425']")).click();

// Switch back to parent window, if required.
driver.switchTo().window(parentwindow);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章