我是C#和Selenium WebDriver的新手.
我知道如何在下拉列表中选择/单击某个选项,但在此之前我遇到了问题.由于下拉列表是动态生成的,因此在运行每个案例之前,我必须从列表中获取所有选项/值.
有没有人告诉我如何从下拉列表中获取所有值/选项.我正在使用IE,我没有找到任何支持在Selenium.IE名称空间中为C#获取值/选项的方法的类.
我的例子:列表包含几个时区:
<TD>
<select name = "time_zone">
<option value "-09:00"><script>timezone.Alaska</script></option>
<option value "+00:00"><script>timezone.England</script></option>
<option value "+02:00"><script>timezone.Greece</script></option>
<option value "+05:30"><script>timezone.India</script></option>
</select>
<TD>
Run Code Online (Sandbox Code Playgroud)
这是IE页面中的下拉列表以及如何获取动态生成的时区列表?
我的代码:
IWebElement elem = driver.FindElement(By.XPath("//select[@name='time_zone']"));
List<IWebElement> options = elem.FindElements(By.TagName("option"));
Run Code Online (Sandbox Code Playgroud)
C#只是弹出一个错误:无法隐式将'OpenQA.Selenium.IWebElement'类型转换为'System.Collections.Generic.List'.存在显式转换(您是否错过了演员?).
谢谢.
在官方W3c webdirver文档中,明确指出位置策略是:
State Keyword
CSS selector "css selector"
Link text selector "link text"
Partial link text selector "partial link text"
Tag name "tag name"
XPath selector "xpath"
Run Code Online (Sandbox Code Playgroud)
但是,Selenium的电线协议允许:
class name
css selector
id
name
link text
partial link text
tag name
xpath
Run Code Online (Sandbox Code Playgroud)
在理论中,Selenium的文档已经过时,"真实"的故事在新的规范文档中.然而...
我在最新的Chrome自己的Webdriver上运行了一些测试,我可以确认这一点,name并且class name两者都有效; 但是,它们不符合规格.
我记得在Chromium问题上阅读他们只会实现官方的Webdriver规范.
现在:我知道通用答案,其中"规格并不总是100%遵循"等.但是,我想知道的是:
javascript selenium google-chrome chromium chrome-web-driver
我有一个表格,其中每一行都有一个下载链接,其中包含(部分)自动生成的 id 元素。这样做的原因是实际的 href 元素始终是“#”,因此 id 将下载分开。
我需要在 td 中找到该 id 元素的名称。也就是说:我知道表格行有一个 id 元素,我知道名称的一部分,我需要得到确切的名称。
我一次访问每一行,所以我一次只需要查看一个 td。无需查看整个表格。
当我知道名称时,我很清楚如何查找元素。但是当我只知道类型时找到元素是另一回事。
...
<tr>
<td class="journalTable-journalPost"
<a class="htext-small" href="#" id="downloadJournalPost-345">Download</a>
</td>
</tr>
<tr>
<td class="journalTable-journalPost"
<a class="htext-small" href="#" id="downloadJournalPost-346">Download</a>
</td>
</tr>
...
Run Code Online (Sandbox Code Playgroud)
我在 webdriver 中找不到任何可以让我按类型查找元素的方法。
部分名称会起作用,因为 id 获得名称“downloadJournalPost-xxx”,其中只有 xxx 更改。但是链接文本是我能找到的唯一值,它可以让我搜索部分匹配。
编辑:更完整的标记。
<td class="journalTable-journalpost">
<span class="hb-tekst--sekundar">In <!----><est-ikon class="ng-star-inserted">
<div aria-hidden="true" class="hb-ikon hb-ikon--pil3-inn ">
<svg focusable="false">
<use xlink:href="#ikon-pil3-inn"></use>
</svg>
</div></est-ikon><!----></span>
<span class="hb-tekst--mellomTittel hb-avstandIngen"> Application and attachments</span>
<a class="hb-tekst--liten" href="#" id="lastNedJournalPost-2892">Download journal post</a>
</td>
Run Code Online (Sandbox Code Playgroud) selenium dom selenium-webdriver webdriverwait expected-condition