如何使用 Selenium 搜索、向下箭头并按回车键
How to search, arrow down and press enter with Selenium
我正在尝试搜索一家公司,向下箭头并单击进入 inhersight.com
我有以下代码,但它似乎不起作用:
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)
似乎无法通过 class 名称定位和查找元素。我已经尝试了很多东西,但它仍然不起作用......我迷路了
搜索公司并在 inhersight.com
上单击输入 而不是因为元素是 Auto Suggestions 所以而不是arrow down
您需要引入 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()
您可以避免选择,因为公司名称成为 URL 中查询字符串的一部分,空格被替换为“-”且全部小写。因此,您可以将 .get 指向此格式的 URL。如果找不到公司,您可以添加一些处理。
from selenium import webdriver
company = 'Apple Federal Credit Union' # 'apple'
base = 'https://www.inhersight.com/company/'
url = base + company.replace(' ', '-').lower()
d = webdriver.Chrome()
d.get(url)
#other stuff including handling of company not found (this text appears on the page so not hard)
#d.quit()
我正在尝试搜索一家公司,向下箭头并单击进入 inhersight.com
我有以下代码,但它似乎不起作用:
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)
似乎无法通过 class 名称定位和查找元素。我已经尝试了很多东西,但它仍然不起作用......我迷路了
搜索公司并在 inhersight.com
上单击输入 而不是因为元素是 Auto Suggestions 所以而不是arrow down
您需要引入 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()
您可以避免选择,因为公司名称成为 URL 中查询字符串的一部分,空格被替换为“-”且全部小写。因此,您可以将 .get 指向此格式的 URL。如果找不到公司,您可以添加一些处理。
from selenium import webdriver
company = 'Apple Federal Credit Union' # 'apple'
base = 'https://www.inhersight.com/company/'
url = base + company.replace(' ', '-').lower()
d = webdriver.Chrome()
d.get(url)
#other stuff including handling of company not found (this text appears on the page so not hard)
#d.quit()