无法单击selenium webdriver中的子菜单项

时间:2015-02-28 02:52:00

标签: selenium-webdriver submenu

无法点击Google表单中的子菜单项。 我只能检查元素,直到File-> New

它有一个子菜单,类似于File-> Menu-> Document。屏幕截图here。我无法找到文档的元素定位器,鼠标无法正常工作。

WebElement newfile = driver.findElement(By.xpath("/html/body/div[14]/div[4]/div"));
newfile.click();

WebElement newfile = driver.findElement(By.linkText("New"));

HTML:

<div id="docs-file-menu" role="menuitem" class="menu-button goog-control goog-inline-block" aria-disabled="false" aria-expanded="false" aria-haspopup="true" style="-webkit-user-select: none;">File</div>
<div class="goog-menuitem-content" style="-webkit-user-select: none;"><span aria-label="New, n;" class="goog-menuitem-label" style="-webkit-user-select: none;"><span class="goog-menuitem-mnemonic-hint" style="-webkit-user-select: none;">N</span>ew</span><span class="goog-submenu-arrow" style="-webkit-user-select: none;">►</span></div>

1 个答案:

答案 0 :(得分:0)

虽然这可能不是写这一切的最有效方式,但试一试。它对我有用:

import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains

driver = webdriver.Chrome()
driver.get("https://docs.google.com/forms/create?usp=mkt_forms")

email = driver.find_element_by_xpath("//input[@id='Email']")
time.sleep(1) #it would bug without more time to load... sometimes..
email.send_keys('youremail@gmail.com')
time.sleep(1) #time for email to be entered

password = driver.find_element_by_xpath("//input[@id='Passwd']")
password.send_keys('yourpassword')
time.sleep(1) #time for password to be entered

submit = driver.find_element_by_xpath("//input[@id='signIn']")
submit.click()

time.sleep(2) #give time for Google forms to load
menu = driver.find_element_by_xpath("//div[@id='docs-file-menu']")
ActionChains(driver).click(menu).send_keys(Keys.ARROW_DOWN).send_keys(Keys.ARROW_DOWN).send_keys(Keys.ARROW_DOWN).send_keys(Keys.ARROW_RIGHT).send_keys(Keys.RETURN).perform()

#rejoice

另外,它在Python中。如果您正在使用Java,则应该很容易切换。