如何使用硒在不和谐的网站版本上按“输入冷区按钮”?

时间:2020-02-26 20:21:01

标签: c# selenium selenium-webdriver discord

我尽一切努力使我的代码单击按钮,但是不能。我尝试使用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);
}

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

所需元素是Angular元素,因此要在元素上定位/ class MdeCaseListView(LoginRequiredMixin, ListView): model = Mde template_name = 'mde/mde_case_list.html' def get_queryset(self): pkey = self.kwargs.get("pk") case = Mde.objects.filter(id=pkey).values_list('case_number', flat=True) return Mde.objects.filter(case_number=case[0]) ,需要为click()引入 WebDriverWait ,您可以使用其中一个以下Locator Strategies中的

  • element_to_be_clickable()

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

    XPath