我对c#相当陌生,并编写了一个简单的Selenium代码,该代码将进入 www.asos.com。然后,我通过找到 xpath 单击右侧的国家/地区。当我单击国家/地区时,我想将国家/地区更改为"印度",将货币更改为"USD",然后选择我的首选项。
我收到driver.FindElement
,SelectElement
的异常,SelectByValue
以前使用java时我没有收到这些异常。
我看到的异常driver
= 当前上下文中不存在名称驱动程序
SelectElement
= 找不到类型或命名空间名称"选择元素">
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 Exercise1
{
class Program
{
static void Main(string[] args)
{
IWebDriver webDriver = new ChromeDriver(@"Path to my chrome driver defined here");
webDriver.Navigate().GoToUrl("www.asos.com");
driver.FindElement(By.XPath("//*[@id="chrome - header"]/header/div[2]/div/ul/li[3]/div/button')]")).Click();
var country = driver.FindElement(By.Id("country"));
var select_country = new SelectElement(country);
select_country = SelectByValue("India");
var currency = driver.FindElement(By.Id("currency"));
var select_currency = new SelectElement(currency);
select_currency = SelectByValue("$ USD");
driver.FindElement(By.XPath("//*[@id="chrome - header"]/header/div[5]/div[2]/div/section/form/div[3]/button")).Click();
}
}
驱动程序。FindElement(By.XPath("//*[@id="chrome - header"]/header/div[5]/div[2]/div/section/form/div[3]/button"((.点击((;
引号中的错误,请尝试:
driver.FindElement(By.XPath("//*[@id='chrome - header']/header/div[5]/div[2]/div/section/form/div[3]/button")).Click();
在 html 中使用选项的值。同时在选择元素上调用选择。例如:
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 Exercise1
{
class Program
{
static void Main(string[] args)
{
IWebDriver driver= new ChromeDriver(@"Path to my chrome driver defined here");
driver.Navigate().GoToUrl("www.asos.com");
driver.FindElement(By.XPath("//*[@id='chrome - header']/header/div[2]/div/ul/li[3]/div/button')]")).Click();
var country = driver.FindElement(By.Id("country"));
var select_country = new SelectElement(country);
select_country.SelectByValue("IN");
var currency = driver.FindElement(By.Id("currency"));
var select_currency = new SelectElement(currency);
select_currency.SelectByValue("2");
driver.FindElement(By.XPath("//*[@id='chrome - header']/header/div[5]/div[2]/div/section/form/div[3]/button")).Click();
}
}
您可能还希望投入一些等待时间,以便有时间加载页面。