How can I identify the element containing the link to my linkedin profile after I logged in using selenium.webdriver?

廉价感情. 提交于 2021-02-11 14:19:35

问题


I have written a script (python) to login to my LinkedIn page (feed) and then I want the script to take me to my profile page. But I cannot capture the element with the link (it keeps changing its id with every restart of the browser). Obviously, I know the link but I would like for the script to be able to capture it.

This is the code I have so far:

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)

this takes me to my feed page.

Any ideas?

thank you


回答1:


The a element of interest does have a changing id="emberXX", but you can use the parent keyword from selenium on its div child:

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

should take you to your profile.




回答2:


In my case ids of the elements are not getting changed. But still you can try with xpath which does not contain id.

I hope following code lines will help:

menu_icon = driver.find_element_by_xpath(//button[@data-control-name='nav.settings']//child::span[1]) menu_icon.click()

profile_link = driver.find_element_by_xpath(//button[@data-control-name='nav.settings']//following-sibling::div[1]) profile_link.click()



来源:https://stackoverflow.com/questions/64296130/how-can-i-identify-the-element-containing-the-link-to-my-linkedin-profile-after

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!