我正在尝试使用Python学习Selenium webdriver。我的测试用例代码看起来是:
import unittest
#import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
#from selenium.webdriver.common.action_chains import ActionChains
class FindRouteToBerlin(unittest.TestCase):
# Start Firefox
def setUp(self):
self.driver = webdriver.Firefox()
def test_find_route_to_berlin(self):
driver = self.driver
# Open HERE Maps
driver.get('http://wego.here.com')
self.assertIn("HERE WeGo", driver.title)
# Go to Search field and fill with city
search = driver.find_element_by_xpath('//*[@id="searchbar"]/div/div/input')
search.send_keys('Berlin')
search.send_keys(Keys.RETURN)
# Press "Arrow"
arrow = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, '/html/body/div[1]/div[6]/div/div/div[1]/div[2]/button')))
arrow.click()
# Enter address
address = driver.find_element_by_xpath('//*[@id="itinerary_item_input_0"]')
address.send_keys('Szczecin')
address.send_keys(Keys.RETURN)
"""actions = ActionChains(driver)
actions.move_to_element(address)
actions.send_keys(Keys.RETURN)
actions.perform()"""
#def tearDown(self):
# self.driver.quit()
if __name__ == "__main__":
unittest.main()
填写文字" Berlin"并且没有任何问题。但是当第二步到来时,看起来Keys.RETURN
在输入" Szczecin"之后没有被执行。
我试图用一些ActionChains
来解决这个问题,但在.perform()
后我出错了,我已经读过这些问题与Selenium / Mozilla中的错误有关。
我的ActionChains代码是:
actions = ActionChains(driver)
actions.move_to_element(address)
actions.send_keys(Keys.RETURN)
actions.perform()
错误:消息:POST / session / 1caed99c-1577-4f1d-804b-4ee397d8750b / moveto不符合 已知命令
有什么建议我该如何解决这个问题?
答案 0 :(得分:0)
您可以尝试从下拉建议中选择第一个元素,而不是使用Keys.RETURN
:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC
address = driver.find_element_by_xpath('//*[@id="itinerary_item_input_0"]')
address.send_keys('Szczecin')
first_suggestion = wait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//div[contains(@class, 'dropdown_list_item')]")))
first_suggestion.click()
答案 1 :(得分:0)
以下是为我工作的:
$value->PHP