调用点击方法时元素不可见
Element not visible when calling click method
我正在尝试使用以下代码打开 </li>
元素:
DropDownElementByText("language-dd", "Englisch");
public void DropDownElementByText(string elementId, string text)
{
using (var dr = new ChromeDriver {Url = "https://www.startpage.com/deu/advanced-search.html"})
{
dr.Manage().Window.Maximize();
var wait = new WebDriverWait(dr, TimeSpan.FromSeconds(10));
IWebElement languageDropDown = wait.Until(d => d.FindElement(By.XPath("//div[@id='" + elementId + "']/ul")));
//new Actions(dr).MoveToElement(languageDropDown).Perform();
ReadOnlyCollection<IWebElement> languages = languageDropDown.FindElements(By.TagName("li"));
foreach (IWebElement li in languages)
{
IWebElement a = dr.FindElement(By.XPath("//div[@id='" + elementId + "']/ul/li/a[text()='" + text + "']"));
if (a != null)
{
new Actions(dr).MoveToElement(li).Perform();
//li.Click();
li.SendKeys(Keys.Enter);
break;
}
}
}
}
但是 li.SendKeys(Keys.Enter)
或 li.Click()
都给我“测试方法 SeleniumTests.SeleniumTests.TestMethod1 抛出异常:
OpenQA.Selenium.ElementNotVisibleException:元素不可见
我不确定你为什么要创建 Iwebelement a
以及你为什么要使用 SendKeys
,如果点击有效的话。
这是更新代码(对我有用):
var dropdown = Driver.FindElement(By.XPath("//*[@id='language-dd']"));
var coll = Driver.FindElements(By.XPath("//*[@id='language-dd']/ul/li"));
foreach (var item in coll)
{
dropdown.Click();
item.WaitForElementToBeClickable(30);
item.Click();
}
public static void WaitForElementToBeClickable(this IWebElement element, int timeout)
{
new WebDriverWait(Driver, TimeSpan.FromSeconds(timeout)).Until(ExpectedConditions.ElementToBeClickable(element));
}
我正在尝试使用以下代码打开 </li>
元素:
DropDownElementByText("language-dd", "Englisch");
public void DropDownElementByText(string elementId, string text)
{
using (var dr = new ChromeDriver {Url = "https://www.startpage.com/deu/advanced-search.html"})
{
dr.Manage().Window.Maximize();
var wait = new WebDriverWait(dr, TimeSpan.FromSeconds(10));
IWebElement languageDropDown = wait.Until(d => d.FindElement(By.XPath("//div[@id='" + elementId + "']/ul")));
//new Actions(dr).MoveToElement(languageDropDown).Perform();
ReadOnlyCollection<IWebElement> languages = languageDropDown.FindElements(By.TagName("li"));
foreach (IWebElement li in languages)
{
IWebElement a = dr.FindElement(By.XPath("//div[@id='" + elementId + "']/ul/li/a[text()='" + text + "']"));
if (a != null)
{
new Actions(dr).MoveToElement(li).Perform();
//li.Click();
li.SendKeys(Keys.Enter);
break;
}
}
}
}
但是 li.SendKeys(Keys.Enter)
或 li.Click()
都给我“测试方法 SeleniumTests.SeleniumTests.TestMethod1 抛出异常:
OpenQA.Selenium.ElementNotVisibleException:元素不可见
我不确定你为什么要创建 Iwebelement a
以及你为什么要使用 SendKeys
,如果点击有效的话。
这是更新代码(对我有用):
var dropdown = Driver.FindElement(By.XPath("//*[@id='language-dd']"));
var coll = Driver.FindElements(By.XPath("//*[@id='language-dd']/ul/li"));
foreach (var item in coll)
{
dropdown.Click();
item.WaitForElementToBeClickable(30);
item.Click();
}
public static void WaitForElementToBeClickable(this IWebElement element, int timeout)
{
new WebDriverWait(Driver, TimeSpan.FromSeconds(timeout)).Until(ExpectedConditions.ElementToBeClickable(element));
}