[英]How do I press "Enter chill zone button" on the website version of the discord using selenium?
I tried everything to make my code click the button but it can't.我想尽一切办法让我的代码点击按钮,但它不能。 I tried using xpath, id, class.
我尝试使用 xpath、id、class。 Neither of them worked.
他们都没有工作。 Does anyone have an idea for this to work?
有没有人有这个想法?
HTML: 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>
My code for finding the button looks like this:我用于查找按钮的代码如下所示:
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);
}
You can try to reach the element by type attribute.您可以尝试通过 type 属性访问元素。 Like this:
像这样:
"//button[@type='submit']"
The desired element is an Angular element so to locate/ click()
on the element you need to induce WebDriverWait for the element_to_be_clickable()
and you can use either of the following Locator Strategies :所需的元素是一个Angular元素,因此要在元素上定位/
click()
,您需要为element_to_be_clickable()
引入WebDriverWait ,您可以使用以下任一定位器策略:
CssSelector
: CssSelector
:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("button[type='submit'] div[class^='contents']"))).Click();
XPath
: 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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.