23

私はhtml hrefリンクを持っています

<a href="/docs/configuration">App Configuration</a>

Selenium を使用して、リンクをクリックする必要があります。現在、私は以下のコードを使用しています -

Driver.findElement(By.xpath("//a[text()='App Configuration']")).click(); 

しかし、それはページにリダイレクトされていません。以下のコードも試しました-

Driver.findElement(By.xpath(//a[@href ='/docs/configuration']")).click();

しかし、これは例外をスローしています-

org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with
Command duration or timeout: 13 milliseconds

リンクが表示され、ページが完全に読み込まれます。コードの何が問題なのかわかりません。

4

11 に答える 11

11

使用する

driver.findElement(By.linkText("App Configuration")).click()

その他のアプローチは

JavascriptLibrary jsLib = new JavascriptLibrary(); 
jsLib.callEmbeddedSelenium(selenium, "triggerMouseEventAt", elementToClick,"click", "0,0");

また

((JavascriptExecutor) driver).executeScript("arguments[0].click();", elementToClick);

詳細な回答については、この投稿を表示してください

于 2015-06-04T05:00:14.400 に答える
2

aタグが隠れているようです。Selenium は非表示の要素と対話できないことに注意してください。Javascriptその場合の唯一のオプションです。

By css = By.cssSelector("a[href='/docs/configuration']");
WebElement element = driver.findElement(css);
((JavascriptExecutor)driver).executeScript("arguments[0].click();" , element);
于 2015-06-04T05:02:49.287 に答える
0

この方法を使用できます:

リンクについてはlinkText();、他のどのロケーターよりも効果的です。

driver.findElement(By.linkText("App Configuration")).click();
于 2016-11-22T07:47:58.683 に答える
0

App Configurationclick()としてテキストを含む要素にするには、次のロケーター戦略のいずれかを使用できます。

  • linkText:

    driver.findElement(By.linkText("App Configuration")).click();
    
  • cssSelector:

    driver.findElement(By.cssSelector("a[href='/docs/configuration']")).click();
    
  • xpath:

    driver.findElement(By.xpath("//a[@href='/docs/configuration' and text()='App Configuration']")).click();
    

理想的には、要素に対してWebDriverWaitclick()誘導する必要があり、次のLocator Strategiesのいずれかを使用できます。elementToBeClickable()

  • linkText:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.linkText("App Configuration"))).click();
    
  • cssSelector:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a[href='/docs/configuration']"))).click();
    
  • xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@href='/docs/configuration' and text()='App Configuration']"))).click();
    

参考文献

関連する詳細な議論のいくつかは、次の場所にあります。

于 2020-11-16T10:49:49.793 に答える
-1

次のように使用できますxpath。これを試してください:

driver.findElement(By.xpath("(.//[@href='/docs/configuration'])")).click();
于 2018-02-22T05:54:29.723 に答える