Selenium c#查找元素选择元素异常
我对c#相当陌生,并编写了一个简单的selenium代码,该代码将发布到www.asos.com上。然后通过查找xpath,单击右侧的国家。当我点击国家时,我想将国家更改为“印度”,货币更改为“美元”,然后选择我的首选项 我在以前使用java时遇到了Selenium c#查找元素选择元素异常,c#,selenium,selenium-webdriver,selenium-chromedriver,C#,Selenium,Selenium Webdriver,Selenium Chromedriver,我对c#相当陌生,并编写了一个简单的selenium代码,该代码将发布到www.asos.com上。然后通过查找xpath,单击右侧的国家。当我点击国家时,我想将国家更改为“印度”,货币更改为“美元”,然后选择我的首选项 我在以前使用java时遇到了driver.FindElement,SelectElement,SelectByValue的异常,我没有遇到这些异常 我看到的驱动程序的异常情况=驱动程序名称在当前上下文中不存在 SelectElement=找不到类型或命名空间名称“SelectE
driver.FindElement
,SelectElement
,SelectByValue
的异常,我没有遇到这些异常
我看到的驱动程序的异常情况
=驱动程序名称在当前上下文中不存在
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();
}
}
在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();
}
}
您可能还需要进行一些等待,以便给页面加载时间
findelelement(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();
谢谢,但是driver.FindElement、SelectElement和SelectByValue都抛出了exception@J.dockster请查看我的选择国家/地区。选择添加。你能发布你收到的异常吗?所有xpathPerfect都一样,修复了这个异常