【问题标题】:How to click svg tag with Selenium Webdriver and .Net core?如何使用 Selenium Webdriver 和 .Net 内核单击 svg 标签?
【发布时间】:2021-06-27 16:49:01
【问题描述】:

我正在尝试单击“svg”标签以将图像添加到 instagram,但我不能。代码中存在延迟,操作未发生。
我的项目使用:

  • Selenium 网络驱动程序
  • Chrome 驱动程序
  • .net core 3.1

            <div class="q02Nz _0TPg" data-testid="new-post-button" role="menuitem" tabindex="0">
                <svg aria-label="Nova publicação" class="_8-yf5 " fill="#262626" height="24" viewBox="0 0 48 48" width="24">
                    <path d="M31.8 48H16.2c-6.6 0-9.6-1.6-12.1-4C1.6 41.4 0 38.4 0 31.8V16.2C0 9.6 1.6 6.6 4 4.1 6.6 1.6 9.6 0 16.2 0h15.6c6.6 0 9.6 1.6 12.1 4C46.4 6.6 48 9.6 48 16.2v15.6c0 6.6-1.6 9.6-4 12.1-2.6 2.5-5.6 4.1-12.2 4.1zM16.2 3C10 3 7.8 4.6 6.1 6.2 4.6 7.8 3 10 3 16.2v15.6c0 6.2 1.6 8.4 3.2 10.1 1.6 1.6 3.8 3.1 10 3.1h15.6c6.2 0 8.4-1.6 10.1-3.2 1.6-1.6 3.1-3.8 3.1-10V16.2c0-6.2-1.6-8.4-3.2-10.1C40.2 4.6 38 3 31.8 3H16.2z">
                    </path>
                    <path d="M36.3 25.5H11.7c-.8 0-1.5-.7-1.5-1.5s.7-1.5 1.5-1.5h24.6c.8 0 1.5.7 1.5 1.5s-.7 1.5-1.5 1.5z">
                    </path>
                    <path d="M24 37.8c-.8 0-1.5-.7-1.5-1.5V11.7c0-.8.7-1.5 1.5-1.5s1.5.7 1.5 1.5v24.6c0 .8-.7 1.5-1.5 1.5z">
                    </path>
                </svg>
            </div>

【问题讨论】:

    标签: c# selenium-webdriver web-scraping .net-core selenium-chromedriver


    【解决方案1】:

    您不必单击svg 元素。
    您应该单击外部按钮:

    driver.FindElement(By.XPath("//div[@data-testid='new-post-button']")).Click();
    

    只是为了让你知道。如果您仍想点击 svg 元素,您也可以这样做:

    driver.FindElement(By.XPath("//div[@data-testid='new-post-button']/*[name()='svg']")).Click();
    

    【讨论】:

    • 如果我使用driver.FindElement(By.XPath("//div[@data-testid='new-post-button']")).Click();,则会发生异常:“对远程 WebDriver 服务器的 URL localhost:50039/session/c152f14670c2d538934e5b59523f59ae/… 的 HTTP 请求在 60 秒后超时。”如果我使用 driver.FindElement(By.XPath("//div[@data-testid='new-post-button']/*[name()='svg']")).Click(); 发生:“对远程 WebDriver 服务器的 URL localhost:50039/session/c152f14670c2d538934e5b59523f59ae/… 的 HTTP 请求在 60 秒后超时。”
    • 也许 iframe 在那里?你能分享一个指向那个页面的链接吗?
    • 我在这里放了一个更大的sn-p代码:jsfiddle.net/Mil1000/oe91fs3L/1
    • 作为其他用户的信息,要在 instagram 中访问此代码,必须单击 f12,然后单击 f5。
    • 请问有什么意见吗?
    猜你喜欢
    • 2023-03-16
    • 2017-06-09
    • 1970-01-01
    • 2020-12-22
    • 2017-10-31
    • 2019-12-10
    • 1970-01-01
    • 2016-12-08
    相关资源
    最近更新 更多