【问题标题】:Click button in using VBA Chrome selenium单击使用 VBA Chrome selenium 的按钮
【发布时间】:2022-01-11 00:36:43
【问题描述】:

晚安
我想使用 chrome selenium 点击上传图片按钮
我尝试过编码,但它不起作用

Public Sub CallChrome1()
    Dim ch As Selenium.ChromeDriver
    Dim wsh As Object
    Dim strFolder As String
    
     Set wsh = CreateObject("Wscript.Shell")
    strFolder = wsh.regread("HKCU\Volatile Environment\LOCALAPPDATA") & "\Google\Chrome\User Data"
    Set ch = New Selenium.ChromeDriver
    
    ch.AddArgument ("user-data-dir=" & strFolder)
    ch.AddArgument ("profile-directory=Default")
    ch.Start
    ch.Get "https://sellercenter/apps/product/publish"
    Application.Wait (Now + TimeValue("0:00:05"))
    
    ch.ExecuteScript "window.open(arguments[0])", "https://sellercenter/apps/mediacenter"
    ch.SwitchToNextWindow
    Application.Wait (Now + TimeValue("0:00:10"))
    ch.FindElementByXPath("//a[@class='next-btn next-medium next-btn-primary' and text()='Upload Image']").Click
       MsgBox "Press OK to Close"
End Sub







这是html代码

<button type="button" class="next-btn next-medium next-btn-primary" role="button">Upload Image</button>


非常感谢有关如何在 Excel VBA 中使用 Selenium 单击此按钮的任何建议,谢谢。

【问题讨论】:

    标签: vba selenium selenium-webdriver xpath css-selectors


    【解决方案1】:

    在编写选择器时,在 selenium 中的第一件事是,始终检查它们的出现。如果您想单击特定元素,则选择器应显示 1 个匹配计数。 现在在这里您使用了类值,它看起来有多个匹配节点,并且当有多个类时,您可以在 selenium 中以这种方式使用类值(在类值中更多的单词由空格分隔)。

    所以在这里我建议使用下面的 xpath,在你的代码中使用它之前,验证它是否显示一个匹配的节点-

    //button[text()='Upload Image']
    

    【讨论】:

    • SelectorsHub - XPath 插件不工作
    • 您遇到什么错误?您可以在单击上传图片按钮之前添加 10 秒等待,看看它是否有效。
    • 没有错误但没有点击
    • 在该页面上显示了多少匹配此 xpath- //button[text()='Upload Image'] 请确保它找到 1 个匹配节点。如果可能,请提供您的页面链接,我会帮助您解决问题。
    • Hello @ SelectorsHub - XPath Plugin 我在没有 Profile 的情况下尝试 Selenium 后,代码成功,但是,为什么如果在 selenium chrome Profile 上运行,代码不起作用,有什么问题吗?
    【解决方案2】:

    By.CLASS_NAME 接受单个类名。所以你不能在 FindElementByClass() 中传递多个类名。

    到元素上传图片上的click(),您可以使用以下Locator Strategies

    • 使用FindElementByCss

      Application.Wait (Now + TimeValue("0:00:10"))
      ch.FindElementByCss("a.next-btn.next-medium.next-btn-primary").Click
      
    • 使用FindElementByXPath

      Application.Wait (Now + TimeValue("0:00:10"))     
      ch.FindElementByXPath("//a[@class='next-btn next-medium next-btn-primary' and text()='Upload Image']").Click
      

    【讨论】:

    • 谢谢上面的代码我都试过了,还是不行
    • @anggun 查看更新的答案并告诉我状态。
    • @DebanjaB code not working 查看我上面更新的代码
    • @anggun 你到底卡在哪里了?你看到了什么错误?
    • 找不到元素link
    猜你喜欢
    • 2022-01-26
    • 1970-01-01
    • 2022-08-05
    • 2022-01-19
    • 2020-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多