gpt4 book ai didi

python - 如何使用 Selenium 搜索、向下箭头并按回车键

转载 作者:行者123 更新时间:2023-11-28 19:28:41 25 4
gpt4 key购买 nike

我正在尝试搜索一家公司,在 inhersight.com 上向下箭头并单击 enter

我有以下代码,但它似乎不起作用:

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

driver.get("https://www.inhersight.com/companies")
elem = driver.find_element_by_class_name("open-search.small-hide.margin-right-20.icon-36.icon-search.reverse.cursor-pointer").click()
elem.send_keys("Apple")
elem.send_keys(Keys.ARROW_DOWN)

好像不能通过类名来定位和查找元素。我已经尝试了很多东西,但它仍然不起作用......我迷路了

最佳答案

inhersight.com搜索一家公司并单击 enter 而不是因为元素是 Auto Suggestions 所以不是 向下箭头,您需要诱导 WebDriverWait 以使所需的元素可点击,您可以使用以下解决方案:

  • 代码块:

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC

    options = Options()
    options.add_argument('start-maximized')
    options.add_argument('disable-infobars')
    options.add_argument('--disable-extensions')
    driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
    driver.get("https://www.inhersight.com/companies")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".open-search.small-hide.margin-right-20.icon-36.icon-search.reverse.cursor-pointer"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@placeholder='Search women-rated companies']"))).send_keys("Apple")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//li[contains(@class,'select2-highlighted')]/div[@class='select2-result-label']/div[@class='weight-medium']"))).click()

关于python - 如何使用 Selenium 搜索、向下箭头并按回车键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53684678/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com