python Selenium在某行代码后的工作方式不同

lzfw57am  于 2023-06-20  发布在  Python
关注(0)|答案(2)|浏览(113)

我正在尝试制作一个自动化的LinkedIn应用程序机器人。基本的想法是登录,搜索营销工作,点击'容易申请'过滤器,然后申请到列出的工作,一个接一个。
不过,我只完成了一半。我可以进入申请页面,然后输入我的电话号码。但是,只有排除最后两行代码(在下面给出的代码块的注解中给出)。当我将最后两行代码也设置为可执行代码时,在搜索第一个简单的应用按钮时,执行并不正确。
(我打算使用此代码仅用于学习目的,没有其他)

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import dotenv
import os

dotenv.load_dotenv()

driver = webdriver.Firefox()
driver.get("https://www.linkedin.com/feed/")

email = os.getenv('email')
password = os.getenv('password')

login_button = driver.find_element(By.LINK_TEXT,'Sign in')
login_button.click()

driver.implicitly_wait(10)

email_input = driver.find_element(By.XPATH,'//*[@id="username"]')
password_input = driver.find_element(By.XPATH,'//*[@id="password"]')
signin_button = driver.find_element(By.XPATH,'/html/body/div/main/div[2]/div[1]/form/div[3]/button')

email_input.send_keys(email)
password_input.send_keys(password)
signin_button.click()

driver.implicitly_wait(15)
search_button = driver.find_element(By.XPATH,'/html/body/div[5]/header/div/div/div/div[1]/input')
search_button.send_keys("Marketing")
search_button.send_keys(Keys.ENTER)

driver.implicitly_wait(10)

easyapplybutton = driver.find_element(By.XPATH,'/html/body/div[5]/div[3]/div[2]/div/div[1]/main/div/div/div[1]/div/ul[1]/li[2]/a') 
easyapplybutton.click()
print('s')

driver.implicitly_wait(5)

easyapplybutton2 = driver.find_element(By.XPATH,"//div[@class='jobs-apply-button--top-card']/button[@class='jobs-apply-button artdeco-button artdeco-button--3 artdeco-button--primary ember-view']")
easyapplybutton2.click()

phonenumber = driver.find_element(By.TAG_NAME,'input')
phonenumber.send_keys(os.getenv('phonenumber'))

# nextbutton = driver.find_element(By.CSS_SELECTOR,'[aria-label=Continue to next step]')
# nextbutton.click()
ccrfmcuu

ccrfmcuu1#

这可能与您的选择器有关吗?我想这可能是因为报价。
试着像下面这样使用它:

nextbutton = driver.find_element(By.CSS_SELECTOR,"[aria-label='Continue to next step']")
lrl1mhuk

lrl1mhuk2#

就像你的其他xpath一样,你需要在双引号内传递css_selector,即:"..."和单引号内的属性值,即'...'如下:

nextbutton = driver.find_element(By.CSS_SELECTOR, "[aria-label='Continue to next step']")

将两行代码优化为一行:

driver.find_element(By.CSS_SELECTOR, "[aria-label='Continue to next step']").click()

相关问题