【问题标题】:How do I press "Enter chill zone button" on the website version of the discord using selenium?如何使用 selenium 在不和谐的网站版本上按“进入寒冷区域按钮”?
【发布时间】:2020-06-10 19:12:28
【问题描述】:

我尽一切努力让我的代码点击按钮,但它不能。我尝试使用 xpath、id、class。他们都没有工作。有没有人有这个想法?

HTML:

  <div data-focus-lock-disabled="false" class="inner-1ilYF7">
    <form class="modal-yWgWj- container-14fypd sizeSmall-1jtLQy fullscreenOnMobile-1aglG_">
        <div class="scrollerWrap-2lJEkd content-1EtbQh scrollerThemed-2oenus themeGhostHairline-DBD-2d">
            <div class="scroller-2FKFPG systemPad-3UxEGl inner-ZyuQk0 content-dfabe7">
                <h2 class="h2-2gWE-o title-3sZWYQ defaultColor-1_ajX0 title-18-Ds0 marginBottom20-32qID7 marginTop8-1DLZ1n">WOAH THERE. WAY TOO SPICY</h2>
                <div class="body-Mj9Oxz medium-zmzTW- primary-jw0I4K">You're sending messages too quickly!</div>
            </div>
        </div>
                <div class="flex-1xMQg5 flex-1O1GKY horizontalReverse-2eTKWD horizontalReverse-3tRjY7 flex-1O1GKY directionRowReverse-m8IjIq justifyBetween-2tTqYu alignStretch-DpGPf3 wrap-ZIn9Iy footer-3rDWdC" style="flex: 0 0 auto;">
                    <button type="submit" class="primaryButton-2BsGPp button-38aScr lookFilled-1Gx00P colorBrand-3pXr91 sizeXlarge-2yFAlZ grow-q77ONN">
                        <div class="contents-18-Yxp">Enter the chill zone</div>
                    </button>
                </div>
            </form>
        </div>

我查找按钮的代码如下所示:

try
    {
        var element = driver.FindElement(By.XPath("//*[text()='Enter the chill zone']"));

        if (element.Displayed)
        {
            driver.FindElement(By.XPath("/html/body/div/div[2]/div[2]/div[2]/div[3]/form/div[2]/button")).Click();
            Console.WriteLine("Button clicked!");
        }
    }
catch (Exception e)
{
     Console.WriteLine(e);
}

【问题讨论】:

  • 能否分享一下按钮的html。
  • 检查元素是否存在于 iframe 中?
  • 您尝试单击的按钮是您提供的 HTML 中的父 BUTTON 吗?包含带有“进入寒冷区域”文本的 DIV 的那个?
  • 现在检查html
  • @bial654321 iframe 可能比一个 div 标签更全球化。尝试在这个特定页面的整个 DOM 结构中搜索

标签: c# selenium selenium-webdriver discord


【解决方案1】:

您可以尝试通过 type 属性访问元素。像这样:

"//button[@type='submit']"

【讨论】:

  • 问题是我收到一个关于程序找不到文本框(程序应该用字符写入的那个)的错误,因为按钮覆盖了它Unhandled exception. OpenQA.Selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div/div[1]/div/div[2]/div/div/div/div[2]/div[2]/div[2]/div/form/div/div/div/div[3]/div[2]/div"} (Session info: chrome=80.0.3987.122)
  • 看起来这个元素在框架内。请检查您的元素是否位于
  • 现在检查html
【解决方案2】:

所需元素是Angular 元素,因此要在需要为element_to_be_clickable() 诱导WebDriverWait 的元素上定位/click(),您可以使用以下任一Locator Strategies

  • CssSelector:

    new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("button[type='submit'] div[class^='contents']"))).Click();
    
  • XPath:

    new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//button[@type='submit']/div[starts-with(@class,'contents') and text()='Enter the chill zone']"))).Click();
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-05
    • 2014-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-08
    相关资源
    最近更新 更多