Python 3.x Selenium和Python 3:在otto.de上选择搜索框

Python 3.x Selenium和Python 3:在otto.de上选择搜索框,python-3.x,selenium,selenium-webdriver,Python 3.x,Selenium,Selenium Webdriver,我对Python3和网页抓取都是新手,现在有点困了。 我想: 1.选择otto.de上的搜索框。 2.插入我要搜索的产品编号。 3.按enter键或单击搜索按钮。 4.下载以下页面 otto.de上的搜索字段具有以下源代码: <form class="p_form js_searchForm focus" action="/suche" data-article-number-search="/p/search/" autocomplete="off" autocorrect="off"

我对Python3和网页抓取都是新手,现在有点困了。
我想:
1.选择otto.de上的搜索框。 2.插入我要搜索的产品编号。
3.按enter键或单击搜索按钮。 4.下载以下页面

otto.de上的搜索字段具有以下源代码:

<form class="p_form js_searchForm focus" action="/suche" data-article-number-search="/p/search/" autocomplete="off" autocorrect="off" spellcheck="false" role="search">
            <input placeholder="Suchbegriff / Artikelnr. eingeben" data-error="Bitte mind. ein Zeichen eingeben" class="p_form__input js_searchField sanSearchInput" type="text" autocomplete="off" autocorrect="off" maxlength="50" disabled>
            <button class="sanSearchDelBtn p_symbolBtn100--4th" type="reset"><i>X</i></button>
            <button class="js_submitButton sanSearchButton" type="submit" title="Suche" disabled ><span>&raquo;</span></button>
</form>
它向我提供了以下错误消息:

selenium.common.exceptions.InvalidElementStateException: Message: Unable    to clear element that cannot be edited: <form class=
"p_form js_searchForm focus"> 
selenium.common.exceptions.InvalidElementStateException:消息:无法清除无法编辑的元素:

我尝试了许多不同的命令,但我就是无法进入我需要的页面。有人知道如何解决这个问题吗

下面是一个工作代码:

from selenium import webdriver
from selenium.webdriver.support import ui
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time


browser = webdriver.Firefox()
browser.get("http://www.otto.de")

ui.WebDriverWait(browser, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".p_form__input.js_searchField.sanSearchInput"))).send_keys("538707")

ui.WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".js_submitButton.sanSearchButton"))).click()

with open("Productpage.txt", "w", encoding="utf-8") as outfile:

    outfile.write(browser.page_source)

time.sleep(5)

browser.quit()

希望它能帮助你

在发送键之前是否尝试单击它?听起来好像xpath选择了元素。您真正想要选择的(稍后发送密钥)是表单中的元素。非常感谢,这非常有帮助!!好的,太好了!请在我的答案旁边打勾。为了更好地理解你的代码:你为什么使用WebDriverWait和sleep?查找元素和保存页面内容所需的等待时间?1)
WebDriverWait
用于查找元素。这是一个明确的等待。2)
时间。可以删除睡眠(5)
。这只是为了看到最后的结果。
from selenium import webdriver
from selenium.webdriver.support import ui
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time


browser = webdriver.Firefox()
browser.get("http://www.otto.de")

ui.WebDriverWait(browser, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".p_form__input.js_searchField.sanSearchInput"))).send_keys("538707")

ui.WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".js_submitButton.sanSearchButton"))).click()

with open("Productpage.txt", "w", encoding="utf-8") as outfile:

    outfile.write(browser.page_source)

time.sleep(5)

browser.quit()