如何点击href标签?

How to click on the href tag?

我有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']"}

我还使用了其他属性。仅此站点会出现此问题。我无法关注 link 类别。

单击第一个 link //a[@accesskey='1'] 时,会打开一个新的 window。

我们需要切换到那个新的 window 来点击 //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);