Selenium C#:在 Trello 上找不到按钮

问题描述 投票:0回答:1

我正在尝试自动化 Trello Activity 扩展,我正在尝试使用 Selenium 自动按下“导出到 CSV”按钮。

我的代码,我正在尝试使用 xPath 获取按钮:

driver.FindElement(By.XPath("//[@id=\'content\']/div/div[1]/div[1]/div[2]/span[1]/div/button")).Click();

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(20));

wait.Until(ExpectedConditions.ElementExists(By.XPath("/html/body/section/div[1]/ul/li[1]/button")));
driver.FindElement(By.CssSelector("body > section > div:nth-child(1) > ul > li:nth-child(1) > button")).Click();`

这是按钮:

enter image description here

这是它的 html:

enter image description here

我正在尝试自动化 Trello Activity 扩展,我正在尝试使用 Selenium 自动按下“导出到 CSV”按钮。

c# selenium-webdriver xpath css-selectors webdriverwait
1个回答
0
投票

文本为Export to CSV的元素带有

<iframe>
所以你必须:

  • 诱导 WebDriverWait 所需的 frame 可用并切换到它.

  • 诱导WebDriverWait所需的

    ElementToBeClickable()
    .

  • 您可以使用以下任一Locator Strategies

  • 使用CssSelector

    new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.FrameToBeAvailableAndSwitchToIt(By.CssSelector("iframeCssSelector]"));
    new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("body > section > div:nth-child(1) > ul > li:nth-child(1) > button"))).Click();
    
  • 使用XPath

    new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.FrameToBeAvailableAndSwitchToIt(By.XPath("iframeXPath"));
    new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//html/body/section/div[1]/ul/li[1]/button"))).Click();
    
© www.soinside.com 2019 - 2024. All rights reserved.