因此,我正在尝试使用硒在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()
答案 0 :(得分:1)
尝试对搜索输入执行以下步骤,
search = driver.find_elements_by_xpath("//input[@aria-label='Search']").click()
search.send_keys('sports')
search.submit()
答案 1 :(得分:0)
您可能希望更改定位器以匹配以下元素:
<input role="combobox" autocomplete="off" spellcheck="false" aria-autocomplete="list" aria-invalid="false" aria-expanded="false" aria-label="Search" placeholder="Search" type="text">
将所有内容放在一起:
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()