【发布时间】:2018-05-11 08:56:25
【问题描述】:
我刚开始学习 Selenium WebDriver,遇到了一些问题。 我google了很多,但没有成功。
所以,我要编写一个网站解析器。
有一种HTML。
browser view and html
<div class="view-wrapper"> 包括在内
<ul class="sport--list"> 及其包含<li class="sport--block">...</li> 的列表
我正在尝试循环检查每类运动块,并找到包含“Футбол”等关键字的部分
然后,当我找到合适的部分时,我将获取非静态计时器的值,然后将其写入文件。这是我的下一步。我必须首先解决我的第一个麻烦;
主要问题是 . 我怎样才能找到合适的?我写了这段代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
namespace Parser
{
class Program
{
static void Main(string[] args)
{
using (var driver = new ChromeDriver())
{
// Go to the home page
driver.Navigate().GoToUrl("https://www.favorit.com.ua/ru/live/");
// Get the page elements
IList<IWebElement> ClassNamesElements = driver.FindElements(By.ClassName("sport--block"));
for (int i = 0; i < ClassNamesElements.Count; i++)
{
Console.WriteLine(ClassNamesElements[i]);
Console.ReadLine();
}
}
}
}
}
但我不知道如何设置下一个选择条件。就像“其中包括诸如Футбол之类的东西”。 在那之后,我只想在与我的正确运动相对应的 HTML 片段中工作--block
我不能使用元素的 XPath,因为网站不是静态的。并且适当的运动块可以随机出现。
我不需要你代替我编写代码。我只需要一些方向来继续我的谷歌搜索。
我是否选择了正确的方法来解决这个任务(C# + Selenium)? 请给我一些线索或提示。先感谢您。
【问题讨论】:
-
欢迎来到 Stack Overflow。请改进您的问题。您已经尝试过哪些不起作用的方法?你到底想完成什么?
-
这篇文章还在编辑中还是这里有问题?
-
请提供一些额外的代码,以及您尝试过的内容。尝试更准确地了解您的问题。通常你这样做: List
listElements = driver.findElement(By.xpath("yourxpath"));但提供一些代码你试过了,但它失败了,还有一些更多的信息,html... -
非常抱歉,当时我还没有把我的问题描述完。我做到了。
-
志愿者,OP 现在确实使用我们要求的所有必需信息更新了问题。请考虑撤回反对票。 @TheVee 请阅读为什么screenshot of HTML or code or error is a bad idea。考虑使用基于格式化文本的相关 HTML、代码试验和错误堆栈跟踪来更新问题。
标签: c# selenium selenium-webdriver xpath webdriver