我正试图用Selenium自动化一个网站,然而GetAttribute表现得很奇怪。无论我尝试getattributes的元素是什么,我都会得到下面的错误.我不知道为什么它会试图从System.IO中获取一些东西。
我隔离了代码,尝试使用IWebDriver和ChromeDriver,没有任何效果。
ChromeDriver chrome = new ChromeDriver(Util.Util.GetHomeDir() + "/eztools/drivers/");
chrome.Navigate().GoToUrl("https://dfe-portal.svrs.rs.gov.br/NFE/CCC");
Thread.Sleep(5000);
IWebElement el1 = chrome.FindElementById("CodUf");
Log.Debug("el1:" + el1);
Log.Debug("el2:" + el1.TagName);
Log.Debug("el2:" + el1.GetAttribute("data-val"));
错误。
em System.IO.Path.NormalizePath(String path, Boolean fullCheck, Int32 maxPathLength, Boolean expandShortPaths)
em System.IO.Path.InternalGetDirectoryName(String path)
em OpenQA.Selenium.Internal.FileUtilities.GetCurrentDirectory()
em OpenQA.Selenium.Internal.ResourceUtilities.GetResourceStream(String fileName, String resourceId)
em OpenQA.Selenium.Remote.RemoteWebElement.GetAtom(String atomResourceName)
em OpenQA.Selenium.Remote.RemoteWebElement.GetAttribute(String attributeName)
em ezrpa_runner.Cmds.Logic.WebCaptcha.run(CommandDomain cmd) na C:\ez\vs2019\ez-inspect\ezrpa-runner\Cmds\Web\WebCaptcha.cs:linha 44
所需的元素是一个动态元素,所以要定位元素,你必须诱使 WebDriverWait 与 预期条件 设为 ElementToBeClickable()
您可以使用以下任何一种方式 定位策略:
使用 身份证:
//IWebElement el1 = new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.Id("CodUf")));
//Incase you are using Nuget
IWebElement el1 = new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(By.Id("CodUf")));
Log.Debug("el1:" + el1);
Log.Debug("el2:" + el1.TagName);
Log.Debug("el2:" + el1.GetAttribute("data-val"));
使用 名称:
//IWebElement el1 = new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.Name("CodUf")));
//Incase you are using Nuget
IWebElement el1 = new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(By.Name("CodUf")));
Log.Debug("el1:" + el1);
Log.Debug("el2:" + el1.TagName);
Log.Debug("el2:" + el1.GetAttribute("data-val"));
使用 CssSelector:
//IWebElement el1 = new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("select[name='CodUf']")));
//Incase you are using Nuget
IWebElement el1 = new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(By.CssSelector("select[name='CodUf']")));
Log.Debug("el1:" + el1);
Log.Debug("el2:" + el1.TagName);
Log.Debug("el2:" + el1.GetAttribute("data-val"));
使用 XPath:
//IWebElement el1 = new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//select[@name='CodUf']")));
//Incase you are using Nuget
IWebElement el1 = new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(By.XPath("//select[@name='CodUf']")));
Log.Debug("el1:" + el1);
Log.Debug("el2:" + el1.TagName);
Log.Debug("el2:" + el1.GetAttribute("data-val"));
你可以在.ExpectedConditions.ElementIsVisible中找到相关讨论。