使用搜索栏在Linkedin中搜索查询?我正在使用Selenium(在python中)

时间:2019-07-30 21:20:37

标签: python selenium web-scraping

因此,我正在尝试使用硒在LinkedIn上搜索公司名称,但似乎搜索栏上有一堆元素相互重叠。我尝试使用的每个ID或类,都会收到错误:“ AttributeError”。有人可以指导我使用哪个ID或类吗?我真的很感激!

driver = webdriver.Chrome()
driver.get('https://www.linkedin.com/login')


username = 'xxxxxxx'
password = 'xxxxxxx'


nameidElem = driver.find_element_by_id('username')
nameidElem.send_keys(username)
pwdidElem = driver.find_element_by_id('password')
pwdidElem.send_keys(password)
continueElem = driver.find_element_by_class_name("btn__primary--large")
result = continueElem.submit()
time.sleep(5)


search = driver.find_element_by_id("nav-search-artdeco-typeahead").click()
search.send_keys('sports')
search.submit()

2 个答案:

答案 0 :(得分:1)

尝试对搜索输入执行以下步骤,

search = driver.find_elements_by_xpath("//input[@aria-label='Search']").click()
search.send_keys('sports')
search.submit()

答案 1 :(得分:0)

  1. 请考虑使用Explicit Waits以确保在与元素进行交互之前,这些元素是存在/可见/可单击的。查看How to use Selenium to test web applications using AJAX technology文章以了解更多详细信息
  2. 单击元素时,它可能会在DOM中更改其状态,因此您可能需要再次调用find_element函数
  3. 您可能希望更改定位器以匹配以下元素:

    <input role="combobox" autocomplete="off" spellcheck="false" aria-autocomplete="list" aria-invalid="false" aria-expanded="false" aria-label="Search" placeholder="Search" type="text">
    
  4. 将所有内容放在一起:

    nameidElem = WebDriverWait(driver, 10).until(expected_conditions.presence_of_element_located((By.ID, "username")))
    nameidElem.send_keys(username)
    pwdidElem = WebDriverWait(driver, 10).until(expected_conditions.presence_of_element_located((By.ID, "password")))
    pwdidElem.send_keys(password)
    continueElem = WebDriverWait(driver, 10).until(
        expected_conditions.presence_of_element_located((By.CLASS_NAME, "btn__primary--large")))
    result = continueElem.submit()
    
    search = WebDriverWait(driver, 10).until(
        expected_conditions.presence_of_element_located((By.XPATH, "//input[@placeholder='Search']"))).click()
    WebDriverWait(driver, 10).until(
        expected_conditions.element_to_be_clickable((By.XPATH, "//input[@placeholder='Search']"))).send_keys('sports')
    WebDriverWait(driver, 10).until(
        expected_conditions.element_to_be_clickable((By.XPATH, "//input[@placeholder='Search']"))).submit()