问题描述
我是移动自动化的新手。我以为我明白了要点,但我遇到了一个问题。 使用命令 send_keys() 时出现错误 selenium.common.exceptions.InvalidElementStateException:消息:无法将元素设置为“321785214”。您是否与正确的元素进行了交互?
driver = webdriver.Remote("http://localhost:4723/wd/hub",desired_cap)
# Skip fingerprint
driver.find_element_by_xpath("//android.widget.Button[@bounds='[413,1802][668,1877]']").click()
# Click Continue
driver.find_element_by_xpath("//android.widget.Button[@content-desc='Continue']").click()
# Click to input phone number
input_phone_number = driver.find_element_by_xpath("//android.widget.EditText").click()
input_phone_number_edit = driver.find_element_by_class_name("android.widget.EditText")
# Enter phone number
input_phone_number_edit.send_keys("321785214")
driver = webdriver.Remote("http://localhost:4723/wd/hub",desired_cap)
# click to Set up later button
set_up_later_btn = driver.find_element_by_xpath('//android.widget.Button[@content-desc="Set up later"]').click()
input_phone_number = driver.find_element_by_xpath('//android.view.View[@content-desc="Phone number"]').click()
input_phone_number_edit = driver.find_element_by_class_name('android.widget.EditText')
driver.implicitly_wait(50)
input_phone_number_edit.send_keys('178546128')
driver.find_element_by_accessibility_id("Continue").click()
解决方法
您可以使用 ActionChains 类提供的 send_keys 方法代替默认的 send_keys 方法
从 selenium.webdriver 导入 ActionChains
actions = ActionChains(self.driver)
actions.send_keys("321785214")
actions.perform()