如何在selenium python中使驱动程序导航到新页面?



我正在尝试使用selenium和python编写一个脚本来自动化Linkedin上的工作申请。

步骤很简单:

  1. 打开LinkedIn页面,输入id密码登录
  2. 打开https://linkedin.com/jobs并输入搜索关键字和位置并单击搜索(直接打开链接,如https://www.linkedin.com/jobs/search/?geoId=101452733&keywords=python&location=Australia在加载时卡住,可能是由于缺乏上一页的一些帖子信息)
  3. 点击打开工作搜索页面,但这似乎没有更新驱动程序,因为它仍然在前一页搜索。
import selenium
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from bs4 import BeautifulSoup
import pandas as pd
import yaml

driver = webdriver.Chrome("/usr/lib/chromium-browser/chromedriver")

url = "https://linkedin.com/"
driver.get(url)
content = driver.page_source
stream = open("details.yaml", 'r')
details = yaml.safe_load(stream)

def login():
username = driver.find_element_by_id("session_key")
password = driver.find_element_by_id("session_password")
username.send_keys(details["login_details"]["id"])
password.send_keys(details["login_details"]["password"])
driver.find_element_by_class_name("sign-in-form__submit-button").click()


def get_experience():
return "1%C22"

login()

jobs_url = f'https://www.linkedin.com/jobs/'
driver.get(jobs_url)

keyword = driver.find_element_by_xpath("//input[starts-with(@id, 'jobs-search-box-keyword-id-ember')]")
location = driver.find_element_by_xpath("//input[starts-with(@id, 'jobs-search-box-location-id-ember')]")
keyword.send_keys("python")
location.send_keys("Australia")
driver.find_element_by_xpath("//button[normalize-space()='Search']").click()

WebDriverWait(driver, 10)

# content = driver.page_source
# soup = BeautifulSoup(content)
# with open("a.html", 'w') as a:
#     a.write(str(soup))

print(driver.current_url)

司机。current_url返回https://linkedin.com/jobs/而不是https://www.linkedin.com/jobs/search/?geoId=101452733&keywords=python&location=Australia。我尝试将内容打印到一个文件,它确实来自以前的工作页面,而不是来自搜索页面。我也试图从页面搜索元素,如经验和简单的应用按钮,但搜索结果在一个未发现的错误。

我不知道为什么这不起作用。

任何想法?提前感谢

如果直接打开https://www.linkedin.com/jobs/search/?f_AL=True&f_E=2&keywords=python&location=Australia但不打开https://www.linkedin.com/jobs/search/?f_AL=True&f_E=1%2C2&keywords=python&location=Australia

这两个链接的不同之处在于,其中一个只接受一个经验值,而另一个接受两个值。这意味着这可能不是post value的问题。

单击搜索按钮后,在页面因从服务器收到的响应而更改之前,您将立即获取并打印当前URL。
这就是为什么它输出https://linkedin.com/jobs/而不是https://www.linkedin.com/jobs/search/?geoId=101452733&keywords=python&location=Australia的原因,
WebDriverWait(driver, 10)wait = WebDriverWait(driver, 20)不会像time.sleep(10)那样造成任何延迟。
wait = WebDriverWait(driver, 20)只实例化wait对象,WebDriverWait模块/类的实例

相关内容

最新更新