Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用selenium.webdriver登录后,如何识别包含linkedin个人资料链接的元素?_Python_Selenium_Selenium Webdriver_Web Scraping - Fatal编程技术网

Python 使用selenium.webdriver登录后,如何识别包含linkedin个人资料链接的元素?

Python 使用selenium.webdriver登录后,如何识别包含linkedin个人资料链接的元素?,python,selenium,selenium-webdriver,web-scraping,Python,Selenium,Selenium Webdriver,Web Scraping,我已经编写了一个脚本(python)来登录我的LinkedIn页面(feed),然后我希望该脚本将我带到我的个人资料页面。但我无法用链接捕获元素(每次重新启动浏览器时,它都会不断更改其id)。显然,我知道链接,但我希望脚本能够捕获它 这是我目前掌握的代码: import parameters from time import sleep from selenium import webdriver driver = webdriver.Chrome('/Users/uglyr/chromedr

我已经编写了一个脚本(python)来登录我的LinkedIn页面(feed),然后我希望该脚本将我带到我的个人资料页面。但我无法用链接捕获元素(每次重新启动浏览器时,它都会不断更改其id)。显然,我知道链接,但我希望脚本能够捕获它

这是我目前掌握的代码:

import parameters
from time import sleep
from selenium import webdriver

driver = webdriver.Chrome('/Users/uglyr/chromedriver')  
driver.get('https://www.linkedin.com')

username = driver.find_element_by_id('session_key')
username.send_keys(parameters.linkedin_username)

sleep(0.5)    

password = driver.find_element_by_id('session_password')
password.send_keys(parameters.linkedin_password)

sleep(0.5)

log_in_button = driver.find_element_by_class_name('sign-in-form__submit-button')
log_in_button.click()

sleep(1)
这将带我进入我的提要页面

有什么想法吗


谢谢

感兴趣的
a
元素确实有一个不断变化的
id=“emberXX”
,但您可以在其
div
子元素上使用selenium中的
父项
关键字:

profile = driver.find_element_by_xpath("//div[@data-control-name='identity_profile_photo']/parent::a")
profile.click()

应该带你去你的个人资料。

在我的例子中,元素的ID没有改变。但您仍然可以尝试使用不包含id的xpath

我希望以下代码行将有所帮助:

menu_icon=driver.find_element_by_xpath(//button[@data control name='nav.settings']//child::span[1]) 菜单图标。单击()

profile_link=driver.find_element_by_xpath(//button[@data control name='nav.settings']//following sibling::div[1]) 配置文件链接。单击()