问题
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