selenium - 如何在线将价值从网站转移到 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!
}
}
解决方案
您的代码中的此语句
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。
推荐阅读
- i2c - 为什么 `i2c_smbus_write_byte_data` 在 uClinux 2.4 上会返回“不允许操作”?
- html - 在私有 Bitbucket 服务器上呈现单个 HTML 页面
- javascript - viewer.getPlugin(PhotoSphereViewer.MarkersPlugin) 返回 null
- python - cx_Freeze:Python 错误主脚本。找不到模块错误:没有名为 pygments.lexers.python 的模块
- c# - ML.NET 是否支持带有 RBF 内核的 SVM?
- android - 颤振用户身份验证
- qt - Qt Widget 点击 2D pos 到 3D 世界坐标
- php - 将 PDO 准备好的语句放在 DB_Connector 类中的什么位置 - 在构造函数或函数中?
- git - 如何读取子模块的 .git/index 文件
- html - 如何对数字使用“unicode-range”