我有點新硒,所以嘗試後登錄到Quora的網站:的Python和硒:元素目前相互作用,不得操縱
# gets email and password from json
with open('config.json') as f:
login_data = json.load(f)
email = login_data['email']
password = login_data['pass']
email_field_xpath = "//input[@name='email']"
password_field_xpath = "//input[@name='password']"
login_button_xpath = "//input[@value='Login']"
# webdriver's going to wait max 10 seconds for email's field, password field, login button to display
email_field_element = WebDriverWait(driver, 10).until(
lambda driver: driver.find_element_by_xpath(email_field_xpath))
password_field_element = WebDriverWait(driver, 10).until(
lambda driver: driver.find_element_by_xpath(password_field_xpath))
email_field_element.clear()
email_field_element.send_keys(email)
password_field_element.clear()
password_field_element.send_keys(password)
# wait till element is visible before interaction
login_button_element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, login_button_xpath)))
login_button_element.click()
我得到這個錯誤:
Traceback (most recent call last): ...
File "quora.py", line 39, in log_in email_field_element.clear()
File "/home/annieshlepak/work/QuoraAutomatization/env/lib/python3.5/site-packages/selenium/webdriver/remote/webelement.py", line 92, in clear self._execute(Command.CLEAR_ELEMENT) ...
selenium.common.exceptions.InvalidElementStateException: Message: invalid element state: Element is not currently interactable and may not be manipulated
謝謝!指定XPath解決了問題! –
@AnnieShlepak注意'driver.find_element_by'和'EC.presence_of_element_located'並不意味着元素是可見的,只是它存在於DOM中。您需要使用'visibility_of' [預期條件](https://seleniumhq.github.io/selenium/docs/api/py/webdriver_support/selenium.webdriver.support.expected_conditions.html)或'is_displayed( )'功能。 – Guy