How to control Selenium with the "Button" with WPF

Asked 2 years ago, Updated 2 years ago, 64 views

It wasn't a problem when I used it as a console window When I tried to implement it with WPF, I dropped the IWebdriver after running Main Window So the 'Buttons' break the object reference

reference

The source below is the source in question (Button 1, Button 2 handler cannot find the driver)

public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            // Secret Mode Options
            ChromeOptions opt = new ChromeOptions();
            opt.AddArgument("--incognito");

            // Hide command window
            ChromeDriverService ser = ChromeDriverService.CreateDefaultService();
            ser.HideCommandPromptWindow = true;

            // Run selenium
            IWebDriver driver = new ChromeDriver(ser, opt);

        }


        private void b1_Click(object sender, RoutedEventArgs e)
        {            
            driver.Url = "http://naver.com";

        }

        private void b2_Click(object sender, RoutedEventArgs e)
        {
            // Click the TV button
            driver.FindElement(By.XPath("//*[@id='svc.tvcast']/span")).Click();
        }
    }

So

If you pull the IWebdriver out of the main window like below, it works

However, various options cannot be referenced (secret mode, hidden command window)

If you take them all out together, there will be a syntax error if the options come out

How can I solve this problem?

public partial class MainWindow : Window
    {
        // Run selenium -----------------------------------------------------------------------------------
        IWebDriver driver = new ChromeDriver(ser, opt);

        public MainWindow()
        {
            InitializeComponent();

            // Secret Mode Options
            ChromeOptions opt = new ChromeOptions();
            opt.AddArgument("--incognito");

            // Hide command window
            ChromeDriverService ser = ChromeDriverService.CreateDefaultService();
            ser.HideCommandPromptWindow = true;
        }


        private void b1_Click(object sender, RoutedEventArgs e)
        {
            driver.Url = "http://naver.com";
        }

        private void b2_Click(object sender, RoutedEventArgs e)
        {
            // Click the TV button
            driver.FindElement(By.XPath("//*[@id='svc.tvcast']/span")).Click();
        }
    }

wpf selenium

2022-09-22 20:18

1 Answers

I don't know if you've already taken care of it, but... How about doing it like the bottom?

public partial class MainWindow : Window
{
    // selenium -------------- this out into the execution of a scrape @@@@@@@@.
    IWebDriver driver;

    public MainWindow()
    {
        InitializeComponent();

        // Secret mode option
        ChromeOptions opt = new ChromeOptions();
        opt.AddArgument("--incognito");

        Hide // keomaendeuchang
        ChromeDriverService ser = ChromeDriverService.CreateDefaultService();
        ser.HideCommandPromptWindow = true;

        driver = new ChromeDriver(ser, opt);
    }


    private void b1_Click(object sender, RoutedEventArgs e)
    {
        driver.Url = "http://naver.com";
    }

    private void b2_Click(object sender, RoutedEventArgs e)
    {
        Click on the // tv
        driver.FindElement(By.XPath("//*[@id='svc.tvcast']/span")).Click();
    }
}


2022-09-22 20:18

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.