接受使用硒的不和谐邀请

发布于 2025-02-13 17:45:40 字数 982 浏览 3 评论 0原文

我正在尝试创建一个简单的脚本来接受Discord邀请。我已经设法登录到一个帐户,但是我无法接受邀请。

我已经尝试使用XPath同样使用CSS选择器来尝试找到按钮,但是我没有运气(下面看到的示例)。

browser.find_element_by_xpath("/html/body/div[1]/div[2]/div/div/div/div/section/div/button")

browser.find_element_by_class_name('marginTop40-i-78cZ.button-3k0cO7.button-38aScr.lookFilled-1Gx00P.colorBrand-3pXr91.sizeLarge-1vSeWK.fullWidth-1orjjo.grow-q77ONN')

外部HTML是:

<button type="button" class="marginTop40-Q4o1tS button-1cRKG6 button-f2h6uQ lookFilled-yCfaCM colorBrand-I6CyqQ sizeLarge-3mScP9 fullWidth-fJIsjq grow-2sR_-F"><div class="contents-3ca1mk">Accept Invite</div></button>

完整的XPath:

/html/body/div[1]/div[2]/div/div[1]/div/div/div/section/div/button    

我已经在页面底部附上了一个Discord邀请链接。

任何帮助都将不胜感激。

https://discord.com/invite/the1

I'm trying to create a simple script to accept a Discord Invite. I've managed to login to an account however I cannot get it to accept the invite.

I've tried using the XPath aswell as using CSS selector to try and find the button but i'm having no luck (examples seen below).

browser.find_element_by_xpath("/html/body/div[1]/div[2]/div/div/div/div/section/div/button")

browser.find_element_by_class_name('marginTop40-i-78cZ.button-3k0cO7.button-38aScr.lookFilled-1Gx00P.colorBrand-3pXr91.sizeLarge-1vSeWK.fullWidth-1orjjo.grow-q77ONN')

The outer html is:

<button type="button" class="marginTop40-Q4o1tS button-1cRKG6 button-f2h6uQ lookFilled-yCfaCM colorBrand-I6CyqQ sizeLarge-3mScP9 fullWidth-fJIsjq grow-2sR_-F"><div class="contents-3ca1mk">Accept Invite</div></button>

Full XPath:

/html/body/div[1]/div[2]/div/div[1]/div/div/div/section/div/button    

I've attached a discord invite link at the bottom of the page as an example.

Any help would be much appreciated.

https://discord.com/invite/the1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╰ゝ天使的微笑 2025-02-20 17:45:41

所需元素是动态元素,因此要单击您需要诱导的元素 webdriverwait href =“ https://stackoverflow.com/a/54194511/7429447”> emeltage_be_be_clickable() ,您可以使用以下任何一个 定位器策略

  • 使用 css_selector

      webdriverwait(驱动程序,20).ultil(ec.element_to_be_be_clickable(((by.css_selector),“ button [class*='button'button']&gt; div [class^='contents']”))。点击()
     
  • 使用 xpath

      webdriverwait(驱动程序,20).until(ec.element_to_be_be_clickable(((by.xpath,“ // button/div) ']“)))。点​​击()
     
  • 注意:您必须添加以下导入:

     来自selenium.webdriver.support.ui导入webdriverwait
    从selenium.webdriver.common.通过进口
    从selenium.webdriver.support进口预期_conditions作为ec
     

The desired element is a dynamic element, so to 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:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[class*='button'] > div[class^='contents']"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button/div[starts-with(@class, 'contents') and text()='Accept Invite']"))).click()
    
  • Note: You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文