首页 > 解决方案 > 如何在线将价值从网站转移到 Windows 窗体?

问题描述

我正在 Windows 窗体上创建一个应用程序并遇到了麻烦。我每秒都从 Selenium Webdriver 的网站获取一个值,使用 XPath 并将其记录到我的本地字符串中。我得到了计时器的价值。没关系,拿它当一根绳子对我来说很舒服。

所以。我有一个循环,它从网站读取数据,并且每秒也将这个字符串放入 TextBox。

但是在我的循环运行之前,我无法看到文本框的输出。我不想阻止它。

我想在我的应用程序中查看来自在线网站的值。您是否有想法如何每秒刷新表单?如果您需要,我可以提供我的代码。

提前谢谢你,我的大师!

   public static ChromeDriver GetDriver() {
       return new ChromeDriver();
      }

      public static void CheckGame(string URLevent) {
       while (GameIsRun) {
        GetInformation(GetDriver(), URLevent);
        GameIsRun = false;
       }
      }

      static void GetInformation(ChromeDriver driver, string URLevent) {
       driver.Navigate().GoToUrl(URLevent);
       do {
        TimeOfGame = driver.FindElement(By.XPath("html/body/div/div/div//div/" + 
           "div[@class='headroom-wrapper']/div//div[3]/div")).GetAttribute("innerHTML");
        Print print = new Print()
        print.Info(TimeOfGame);
        System.Threading.Thread.Sleep(1000);
       }
       while (TimeOfGame != "90:00");
      }

      public partial class Form1: Form {
       public Form1() {
        InitializeComponent();
       }

       private void Form1_Load(object sender, EventArgs e) {
        Form form = new Form();
       }

       private static Form1 _form = null;
       public static Form1 Form {
        get {
         if (_form == null) {
          _form = new Form1();
         }
         return _form;
        }
       }

       private void button1_Click(object sender, EventArgs e) {
        string eventId = tbEventID.Text; //this works vice versa. It's not my issue.
        string URLevent = "url..." + tbEventID.Text + "/";
        Program.CheckGame(URLevent);
       }
      }

      public class Print {
       public void Info(string TimeOfGame) {
        Form1.Form.tbTeam1.Text = TimeOfGame; 
        //my issue is here. I want to check result at TextBox each second! 
       }
      }

标签: seleniumtextboxwebdriverwindows-forms-designer

解决方案


您的代码中的此语句

TimeOfGame = driver.FindElement(By.XPath("html/body/div/div/div//div/div[@class='headroom-wrapper']/div//div[3]/div")).GetAttribute("innerHTML");

。您应该使用 getText() 方法来提取 HTML 方法的内部文本。此外,为了精确定位 webElement,最好使用聚焦的相对 Xpath 而不是绝对 Xpath。


推荐阅读