[英]How do I press "Enter chill zone button" on the website version of the discord using selenium?
我想盡一切辦法讓我的代碼點擊按鈕,但它不能。 我嘗試使用 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);
}
您可以嘗試通過 type 屬性訪問元素。 像這樣:
"//button[@type='submit']"
所需的元素是一個Angular元素,因此要在元素上定位/ click()
,您需要為element_to_be_clickable()
引入WebDriverWait ,您可以使用以下任一定位器策略:
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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.