繁体   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