直接将链接的 XPATH 粘贴到 selenium 中会指向另一个链接

问题描述 投票:0回答:1

我正在自动完成 Linkden 个人资料的登录和个人资料完成。我已经能够进入主页并通过登录页面。我试图通过模拟链接的点击来进入个人资料页面。为了做到这一点,我使用了直接从检查页面复制的 XPATH,但不是转到配置文件页面,而是转到另一个页面。我不是通过 id 执行此操作,因为我希望此代码适用于插入登录信息的任何人。

下面是代码:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time
import json


with open('information.json', 'r') as file:
    bio_data = json.load(file)


username = ''
password = ''
options = webdriver.ChromeOptions()
# options.add_argument('--headless')  # for headless mode
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))

# Login -------------------------------------------------
try:
    driver.get('https://www.linkedin.com/login')
    username_field = driver.find_element(By.ID, 'username')
    username_field.send_keys(username)

    password_field = driver.find_element(By.ID, 'password')
    password_field.send_keys(password)

    password_field.send_keys(Keys.RETURN)

    time.sleep(5)

# Posting pages -----------------------------------------

    link = driver.find_element(By.XPATH,'/html/body/div[5]/div[3]/div/div/div[2]/div/div/div/div/div/a')
    link.click()


finally:
    driver.quit()

Starting Page

Desired Page

Where it is currently ending

python selenium-webdriver xpath automation selenium-chromedriver
1个回答
0
投票

尝试更好地使用类名来获取元素,然后单击它

© www.soinside.com 2019 - 2024. All rights reserved.