我正在使用 python 和 selenium 來抓取這個網站,我需要覆寫 zipcode 欄位中的默認值。我已經使用了該.clear()方法,然后嘗試使用該send_keys()函式發送我自己的值,但默認的郵政編碼仍保留在輸入欄位中。有誰知道如何解決這個問題?下面是我的代碼:
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from webdriver_manager import driver
from webdriver_manager.chrome import ChromeDriverManager
radius = int(input("Enter the radius: "))
zipcode = int(input("Enter your zipcode"))
url = f'https://www.edmunds.com/cars-for-sale-by-owner/'
options = Options()
options.headless = False
options.add_experimental_option("detach", True)
browser = webdriver.Chrome(ChromeDriverManager().install(), options=options)
browser.maximize_window()
browser.get(url)
browser.set_page_load_timeout(15)
zipcode_form = browser.find_element_by_name('zip')
zipcode_form.clear() #This is meant to clear the default value in the zipcode field
zipcode_form.send_keys(str(zipcode)) #This is enter the zipcode value I provided in the zipcode field
zipcode_form.send_keys(Keys.RETURN) #I did this because there is no button to click after filling the zipcode form on the website and pressing enter works instead
... # The rest of my code which extracts the information I want
我剛開始學習硒,如果我犯了任何愚蠢的錯誤,請原諒我。
uj5u.com熱心網友回復:
可以有多種方法來做到這一點。一種是使用下面定義的 Javascript Executor。“值”包含要輸入的 zip。
element = driver.find_element_by_name('zip')
driver.execute_script("arguments[0].value='" value "';", element)
uj5u.com熱心網友回復:
我建議通過 ID 查找驅動程式元素,因為它對該元素是唯一的:
driver.find_element_by_id('zip').clear()
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/335738.html
