我对C#相当陌生,并编写了一个简单的硒代码,该代码将在www.asos.com上发布。然后,我通过找到xpath单击右侧的国家。当我单击国家时,我想将国家更改为“印度”,将货币更改为“ USD”,然后选择我的偏好。
在使用Java时,我之前遇到driver.FindElement
,SelectElement
,SelectByValue
的异常,但没有得到这些异常。
我看到的driver
异常=当前上下文中不存在名称驱动程序
SelectElement
=找不到类型或名称空间名称'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();
}
}
答案 0 :(得分:0)
使用html中的选项值。还要在select元素上调用select。例如:
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();
}
}
您可能还需要等待一些时间来给页面加载时间。
答案 1 :(得分:0)
driver.FindElement(By.XPath(“ // * [@ id =” chrome-header“] / header / div [5] / div [2] / div / section / form / div [3] / button “))。Click();
带引号的错误,请尝试:
driver.FindElement(By.XPath("//*[@id='chrome - header']/header/div[5]/div[2]/div/section/form/div[3]/button")).Click();