【问题标题】:How to click on the button with text as public as per the HTML through Selenium and C#如何通过 Selenium 和 C# 按照 HTML 格式单击带有文本的按钮
【发布时间】:2018-07-31 02:56:28
【问题描述】:
 <div class="actionButton-container row">
                        <input type="hidden" name="addComment" value="TES-2" id="addCommentTES-2"/>
                          <div class="col-sm-12 col-xs-12">
                            <!-- RESET BUTTON -->
                            <!-- button type="submit" class="btn blue changeBtn" id="Add" type="submit">Add</button>
                            <button type="button" class="btn white changeBtn" id="cancel">Cancel</button> -->



                            <button class="btn blue changeBtn" id="public" onclick="addCommentsForUser()" type="button">Public</button>
                              <button class="btn blue changeBtn" id="inner" onclick="addCommentsForUser()" type="button">Inner</button>
                              <button type="button" class="btn white changeBtn" id="cancel">Cancel</button>
                          </div>

                      </div>

上面是 HTML 代码。我尝试使用下面的代码来获取 selenium 中的按钮并执行单击。它说找不到元素。

driver.FindElement(By.ClassName("actionButton-container")).FindElement(By.Id("public")).Click();

driver.FindElement(By.Id("public")).Click();

有人知道解决办法吗?

【问题讨论】:

  • 你遇到了什么错误?
  • @AnkurSingh 没有这样的元素:无法找到元素:{"method":"id","selector":"public"}
  • 你能分享网址吗?检查它是否在任何框架内
  • 很抱歉无法分享网址,因为它位于私人服务器中
  • IMO,有5个这样的按钮,你要点击哪个按钮?

标签: c# selenium selenium-webdriver xpath webdriver


【解决方案1】:

您可以这样使用,因为您没有共享 URL,因此很难找到结论。但是试试这个:

WebDriverWait wait = new WebDriverWait(driver.driver, TimeSpan.FromSeconds(Time));
wait.Until(ExpectedConditions.ElementToBeClickable(element));

WebDriverWait wait = new WebDriverWait(driver.driver, TimeSpan.FromSeconds(Time));
wait.Until(ExpectedConditions.visibilityOfElementLocated(element));

【讨论】:

  • 试过还是有错误信息:元素不可见
  • 你使用 javascriptExecutor?
  • 还要检查Frame中的元素吗?
  • 是的,我使用 IJavaScriptExecutor。你的意思是什么框架? iFrame 还是其他框架?
  • 它不包含任何 iframe
【解决方案2】:

根据您共享的 HTML,单击带有文本为 Public 的按钮,您需要为所需的 诱导 WebDriverWait >元素可点击,您可以使用以下解决方案:

new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//div[@class='actionButton-container row']//button[@class='btn blue changeBtn' and @id='public']"))).Click();

【讨论】:

  • 尝试了上述解决方案,但它不起作用。错误消息:OpenQA.Selenium.WebDriverTimeoutException:10 秒后超时消息:OpenQA.Selenium.WebDriverTimeoutException:10 秒后超时----> OpenQA.Selenium.NoSuchElementException:没有这样的元素
  • TimeoutExceptionfailed ExpectedConditions 的结果。通过findElement()Thread.sleep() 结合使用来调试您的代码。如果您能够找到该元素,请使用观察结果更新问题。
  • 我尝试使用这种方法进行调试:- IList attachmentList = driver.FindElements(By.XPath("//*[@id='comment-containerTES-2']/ div[14]/div[2]/div/div[2]/div/div")); foreach(附件列表中的 var 元素){ Console.WriteLine(element.Text);输出是 Public Inner Cancel 如何从中获取 Public 按钮
【解决方案3】:
  driver.FindElement(By.XPath("//*[@id='commentSFHD-12']")).SendKeys("TEST Function");
  System.Threading.Thread.Sleep(5000);
  IWebElement ibtn3 = driver.FindElement(By.Id("public"));
  IJavaScriptExecutor executor = (IJavaScriptExecutor)driver;
  executor.ExecuteScript("arguments[0].click();", ibtn3);

通过使用上面的代码,它现在可以正常工作了

【讨论】:

    猜你喜欢
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 2020-11-15
    • 2018-12-17
    • 2019-01-20
    • 2017-05-31
    • 2019-01-06
    • 2022-01-19
    相关资源
    最近更新 更多