我一直在尝试使用Selenium自动化搜索。我只是想搜索术语(比如说Pink Floyd
),但文件类型应该是pdf。以下是我到目前为止所做的事情:
//Query term
WebElement element = driver.findElement(By.name("as_q"));
String finalQuery = "pink floyd";
element.sendKeys(finalQuery);
//File type selection
WebElement elem = driver.findElement(By.id("as_filetype_button"));
elem.sendKeys("Adobe Acrobat pdf (.pdf)");
driver.findElement(By.xpath("/html/body/div[1]/div[4]/form/div[5]/div[9]/div[2]/input[@type='submit']")).click();
这会将术语放在适当的位置,并且文件类型的下拉列表会被扩展,但不会选择pdf选项。有什么帮助吗?
我正在使用Selenium 2.53.0。
修改
以下代码段完全符合此问题的已接受答案。但是,突然之间代码段无法正常工作。我发现这一点有点惊讶。以前,我能够使用以下代码段自动选择PDF,但现在没有选择任何内容。
WebElement element = driver.findElement(By.name("as_q"));
String finalQuery = "pink floyd";
element.sendKeys(finalQuery);
driver.findElement(By.id("as_filetype_button")).click();
driver.findElement(By.xpath("//li[@class=class-name][@value='pdf']")).click();
答案 0 :(得分:1)
我是这样做的,找到与li
和class='goog-menuitem'
匹配的value='pdf'
,我检查了该元素。您可以直接使用value='pdf'
,但只是为了确保我们正在查看我们添加该类的文件类型下拉列表。
driver.findElement(By.id("as_filetype_button")).click();
driver.findElement(By.xpath("//li[@class='goog-menuitem'][@value='pdf']")).click();
你仍然可以用WebElement
声明它,我只是喜欢它的简写。希望这可以帮助。