用硒元素循环浏览列表



我有一个名为dhurl的列表,其中有30个web URL。我正试图循环遍历selenium web驱动程序代码,以便在每次循环时更改URL,以便使用不同的URL执行相同的操作(每个URL中都有不同的项目(。

到目前为止我拥有的代码:

对于我的列表:

//Loop which adds keyName and tokenUrl to list
foreach (JObject item in jArray)
{
var keyNameSm = (string)item.SelectToken("keyName");
var keyName = keyNameSm.Replace("sm-", "");
var tokenUrl = (string)item.SelectToken("token");
dhurl.Add("https://" + keyName + ".bewitchedexample.cloud/?sig=" + tokenUrl);
}

我的Selenium web驱动程序代码:

public void LaunchingDH()
{
driver = GetChromeDriver();
string url = "https://harry.bewitchedexample.cloud/?sig=433435456464546";
Thread.Sleep(1000);
driver.FindElement(By.ClassName("terms-checkbox")).Click();
driver.FindElement(By.ClassName("continue-button")).Click();
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(60));
driver.Close();
}

硒代码中的url(https://harry.bewitchedexample.cloud/?sig=433435456464546"(是硬编码的,我想每次它循环时都将该URL更改为dhurl。

到目前为止我拥有的代码:

public void LaunchDH(List<string> dhurl)
{
foreach (string url in dhurl)
{
driver = GetChromeDriver();
string url = "https://harry.bewitchedexample.cloud/?sig=433435456464546";
driver.Navigate().GoToUrl(url);
Thread.Sleep(1000);
driver.FindElement(By.ClassName("terms-checkbox")).Click();
driver.FindElement(By.ClassName("continue-button")).Click();
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(60));
driver.Close();
}
}

有什么建议我该怎么做?

去掉Selenium部分和其他所有部分,这里的主要任务是在每个循环中将一个项目从list传递到string,并对它们执行一些Selenium操作。

假设你已经准备好了URL的list,下面这个简化的案例只打印到控制台,这样你就可以看到结果:

var url_list = new List<string>()
{
"URL_1", "URL_2", "URL_3"
};
foreach(string row in url_list)
{
string url = $"https://{row}";
Console.WriteLine(url);
}

打印:

https://URL_1
https://URL_2
https://URL_3

使用以下代码:

JObject URLList = JObject.Parse(@"{
URLS: [
{'URLName': 'https://www.google.com/'},
{'URLName': 'https://www.facebook.com'},
{'URLName': 'https://uk.yahoo.com/?p=us'},
]
}");
var dhurl = new List<string>();
foreach (JObject url in URLList)
{
string targetURL = (string)URLList.SelectToken("URLS[url].URLName");
dhurl.Add(targetURL);
},
public void LaunchDH(List<string> dhurl)
{
foreach (string url in dhurl)
{
driver = GetChromeDriver();
driver.Navigate().GoToUrl(url);
Thread.Sleep(1000);
driver.FindElement(By.ClassName("terms-checkbox")).Click();
driver.FindElement(By.ClassName("continue-button")).Click();
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(60));
driver.Close();
}
}

相关内容

  • 没有找到相关文章

最新更新