我怎样才能让 Selenium Web 驱动程序看到 "hidden" 的下拉菜单?

How can I get Selenium Web Driver to see drop downs that are "hidden"?

好吧,也许我只是有点太累了,但出于某种原因,我无法让 selenium 查看仅在单击时显示的下拉选项的 ID。例如,https://accounts.google.com/SignUp。如果你检查出生月份或性别下拉菜单,你可以明白我在说什么......

tl;dr:如何让 selenium 找到隐藏元素的 ID

两件事。在这种情况下,您必须先单击打开性别下拉列表 div,它会填充列表并使选项不隐藏。 Then find a target option with appropriate selector and with sufficient wait time.请注意,我正在使用 explicit wait 等待元素可见。

_driver.FindElement(By.CssSelector("[title='Gender']")).Click();

IWebElement myElement = new WebDriverWait(_driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementIsVisible(By.XPath("//div[@class='goog-menuitem-content'][.='Male']")));
myElement.Click();

如果您是初学者,我不建议您使用 Google 进行任何类型的测试。 Google 的代码很复杂且容易引起误解。寻找另一个学习应用。