Chrome VB.net 和OpenQA Selenium,尝试单击WordPress网站中的按钮

rvpgvaaj  于 2023-04-27  发布在  Go
关注(0)|答案(1)|浏览(95)

我在尝试点击WordPress网站上的按钮时遇到了问题。该网站的代码很难导航。这是我迄今为止的代码。

Imports OpenQA.Selenium
Imports OpenQA.Selenium.Chrome
Imports System.IO

Module Module1
    Sub Main()
        ' Create a new ChromeDriver instance
        Dim driver As New ChromeDriver()
        ' Navigate to the URL
        driver.Navigate().GoToUrl("https://www.airexpert.net/2023dreamteam")
        ' Find the button element by its ID and click it
        Dim bodyElement As IWebElement = driver.FindElement(By.CssSelector("#y9ar97_0_offer"))
        ' Find the button element with the id "choice_57" within the body element
        Dim button As IWebElement = bodyElement.FindElement(By.Id("choice_101"))
        ' Click on the button element
        button.Click()
        ' Quit the driver to close the browser
        driver.Quit()
    End Sub
End Module

我试图点击一个按钮,但导航到它似乎在代码中很难。

kmynzznz

kmynzznz1#

您尝试单击的按钮位于IFRAME中。您必须将上下文切换到该IFRAME才能看到其中的任何内容。您已经找到了IFRAME,但将其命名为bodyElement。我已经更新了下面的代码。

Imports OpenQA.Selenium
Imports OpenQA.Selenium.Chrome
Imports System.IO

Module Module1
    Sub Main()
        ' Create a new ChromeDriver instance
        Dim driver As New ChromeDriver()
        ' Navigate to the URL
        driver.Navigate().GoToUrl("https://www.airexpert.net/2023dreamteam")
        ' Find the IFRAME and switch into it
        .SwitchToFrame driver.FindElement(By.CssSelector("#y9ar97_0_offer"))
        ' Click the button element with the id "choice_57"
        driver.FindElement(By.Id("choice_101")).Click()
        ' Quit the driver to close the browser
        driver.Quit()
    End Sub
End Module

相关问题