簡體   English   中英

為什么我無法從頁面中找到並單擊按鈕/定位元素?

[英]Why I am unable to find and click the button/anchor element from a page?

我一直在嘗試單擊Bydureon 頁面中的按鈕/錨元素,這是我的簡單代碼:

public class RoughTest {

    private WebDriver driver;
    private WebDriverWait wait;

    @BeforeMethod(alwaysRun = true)
    private void setUp() {
        System.setProperty("webdriver.chrome.driver", "src/main/resources/chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        wait = new WebDriverWait(driver, 5);
    }

    @Test
    public void roughTest() throws InterruptedException {

        driver.get("https://www.bydureon.com/bydureon-bcise.html");
        WebElement button = driver.findElement(
                  By.xpath("(//div[@id='doctor-discussion-guide']//a[1]"));
        wait.until(ExpectedConditions.elementToBeClickable(button));
        button.click();
        Thread.sleep(1000);
    }

    @AfterMethod(alwaysRun = true)
    private void tearDown() {
        // close browser
        driver.quit();
    }
}

嘗試使用多種方法找到它但沒有運氣。

//div[@id='doctor-discussion-guide']//a[1]
//a[@href='/content/dam/intelligentcontent/brands/byd/global/about/US-56308-BCI-Doctor-Discussion-Guide.pdf']
(//a[@class='cmp-teaser__action-link'][normalize-space()='Get 4 Key Questions'])[1]
//a[contains(text(),'Get 4 Key Questions')]

總是得到以下異常:

org.openqa.selenium.ElementClickInterceptedException: element click intercepted: Element is not clickable

這是元素的 HTML 代碼

<div class="cmp-teaser__action-container" xpath="1">
<a class="cmp-teaser__action-link" href="/content/dam/intelligentcontent/brands/byd/global/about/US-56308-BCI-Doctor-Discussion-Guide.pdf" style="">
Get 4 Key Questions
<span class="cmp-teaser__actionicon">
<img>
<span></span>
</span>
</a>
<a class="cmp-teaser__action-link" href="/content/dam/intelligentcontent/brands/byd/global/about/US-56308-BCI-Doctor-Discussion-Guide.pdf">
Get 4 Key Questions
<span class="cmp-teaser__actionicon">
<img>
<span></span>
</span>
</a>
</div>

這是元素在頁面中可見的方式:

回答 4 個關鍵問題

最后,在 JavascriptExecutor 的幫助下能夠點擊元素,所以這是我的代碼:

public class RoughTest {

    private WebDriver driver;
    private JavascriptExecutor jse;

    @BeforeMethod(alwaysRun = true)
    private void setUp() {
        System.setProperty("webdriver.chrome.driver", "src/main/resources/chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        jse = (JavascriptExecutor)driver;
    }

    @Test
    public void roughTest() throws InterruptedException {

        driver.get("https://www.bydureon.com/bydureon-bcise.html");
        WebElement button = driver.findElement(By.xpath("(//a[@class='cmp-teaser__action-link'])[3]"));
        
        jse.executeScript("arguments[0].click()", button);
        
        Thread.sleep(1000);
    }

    @AfterMethod(alwaysRun = true)
    private void tearDown() {
        // close browser
        driver.quit();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM