python Selenium不在网页上搜索

egdjgwm8  于 2024-01-05  发布在  Python
关注(0)|答案(2)|浏览(117)

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

  1. from selenium import webdriver
  2. from selenium.webdriver.common.by import By
  3. from selenium.webdriver.common.keys import Keys
  4. #idk what these two lines are, it helps not close the tab immediately.
  5. options = webdriver.ChromeOptions()
  6. options.add_experimental_option("detach", True)
  7. driver = webdriver.Chrome(options=options)
  8. driver.get("https://www.copart.com/")
  9. search = driver.find_element(By.ID, 'mobile-input-search')
  10. search.send_keys("72486533")
  11. search.send_keys(Keys.RETURN)

字符串

kqqjbcuj

kqqjbcuj1#

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

  1. from selenium import webdriver
  2. from selenium.webdriver import Keys
  3. from selenium.webdriver.common.by import By
  4. from selenium.webdriver.support.ui import WebDriverWait
  5. from selenium.webdriver.support import expected_conditions as EC
  6. driver = webdriver.Chrome()
  7. driver.get("https://www.copart.com/")
  8. wait = WebDriverWait(driver, 15)
  9. search = wait.until(EC.visibility_of_element_located((By.ID, 'input-search')))
  10. search.send_keys("72486533")
  11. search.send_keys(Keys.RETURN)
  12. driver.find_element(By.CSS_SELECTOR, 'button[type=submit]').click()
  13. wait.until(EC.visibility_of_element_located((By.CLASS_NAME, 'title-and-highlights')))

字符串

展开查看全部
luaexgnf

luaexgnf2#

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

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

字符串
这需要以下导入:

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

示例

  1. from selenium import webdriver
  2. from selenium.webdriver.common.by import By
  3. from selenium.webdriver.common.keys import Keys
  4. from selenium.webdriver.support.ui import WebDriverWait
  5. from selenium.webdriver.support import expected_conditions as EC
  6. from selenium.common.exceptions import NoSuchElementException
  7. options = webdriver.ChromeOptions()
  8. options.add_experimental_option("detach", True)
  9. driver = webdriver.Chrome(options=options)
  10. driver.get("https://www.copart.com/")
  11. WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '[aria-label="Consent"]'))).click()
  12. search = driver.find_element(By.ID, 'mobile-input-search')
  13. search.send_keys("72486533")
  14. search.send_keys(Keys.RETURN)

展开查看全部

相关问题