python Selenium不在网页上搜索

egdjgwm8  于 11个月前  发布在  Python
关注(0)|答案(2)|浏览(97)

所以我试图在网站www.copart.com/中搜索一辆车作为一个项目的实践。我试图用批号搜索(72486533)在搜索框中,但它没有搜索它由于某种原因,并显示错误.请让我知道这个令人失望的代码有什么问题.(如果有人能推荐一个更新的 selenium 课程,那将是惊人的,我是新的编码)这是我的尝试:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys


#idk what these two lines are, it helps not close the tab immediately.
options = webdriver.ChromeOptions()
options.add_experimental_option("detach", True)

driver = webdriver.Chrome(options=options)

driver.get("https://www.copart.com/")

search = driver.find_element(By.ID, 'mobile-input-search')
search.send_keys("72486533")
search.send_keys(Keys.RETURN)

字符串

kqqjbcuj

kqqjbcuj1#

你依赖于错误的输入。你得到的输入是移动的视图,它是不可见的。对于Web需要的输入有选择器#input-search。此外,我建议等到搜索结果呈现,例如,通过等待元素.title-and-highlights的可见性

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

driver = webdriver.Chrome()

driver.get("https://www.copart.com/")
wait = WebDriverWait(driver, 15)

search = wait.until(EC.visibility_of_element_located((By.ID, 'input-search')))
search.send_keys("72486533")
search.send_keys(Keys.RETURN)
driver.find_element(By.CSS_SELECTOR, 'button[type=submit]').click()
wait.until(EC.visibility_of_element_located((By.CLASS_NAME, 'title-and-highlights')))

字符串

luaexgnf

luaexgnf2#

您可能应该首先处理同意弹出窗口,以与您的搜索进行交互:

WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '[aria-label="Consent"]'))).click()

字符串
这需要以下导入:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException

示例

from selenium import webdriver
from selenium.webdriver.common.by import By
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.common.exceptions import NoSuchElementException

options = webdriver.ChromeOptions()
options.add_experimental_option("detach", True)

driver = webdriver.Chrome(options=options)
driver.get("https://www.copart.com/")

WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '[aria-label="Consent"]'))).click()
search = driver.find_element(By.ID, 'mobile-input-search')
search.send_keys("72486533")
search.send_keys(Keys.RETURN)

相关问题