1

My Code is:

bar = browser.find_element_by_xpath("//*[@id='ember26']/input")

But it changes every time I login. I can login with my codes but inside the LinkedIn, search bar XPath changes, like ;

//*[@id='ember16']/input or something else.

Any help is appreciated.

enter image description here

5
  • I tried to find, for example, by class name but it was useless again as well as by css selector. Mar 29, 2021 at 17:37
  • You've run into the issue of dynamic IDs, so they'll always change the xpath or CSS selector if you base your element on IDs. Are you testing LinkedIn website? Is this for learning practice? If you don't have control of the website, then you can't change the behavior.
    – Lee Jensen
    Mar 29, 2021 at 21:47
  • I am a beginner in Python. But i do what my instructor does. He can get the xpaths easily. Mar 30, 2021 at 7:25
  • 1
    Please add the html.you are trying to inspect
    – PDHide
    Mar 30, 2021 at 10:37
  • linkedin.com/feed Mar 30, 2021 at 17:01

2 Answers 2

2

xpath:

//*[contains(@id,"ember")]/input

or

//*[starts-with(@id,"ember")]/input

css

[id*="ember"]>input

or

[id^="ember"]>input
  • is css for contains and ^ start with for css

you can use any of the above locator to find the search field in linkedin

9
  • Can you explain the difference between //* and // ?
    – vitaliis
    Apr 2, 2021 at 13:32
  • // any where in the preceding node context , eg //div looks div any where under div (not only direct child) /div//span this searches for span tag any where under the /div tag . * means any. //*/div means div tag which is child of any tag , it can be span , div anything
    – PDHide
    Apr 2, 2021 at 15:25
  • Thanks for the explanation.
    – vitaliis
    Apr 2, 2021 at 15:28
  • 1
    I cannot accept, only upvote)
    – vitaliis
    Apr 2, 2021 at 15:29
  • 1
    Ho i thought you are the author , thanks for the upvote :)
    – PDHide
    Apr 2, 2021 at 15:30
0

You can use partial matches with css selectors. So where you have id=ember26 above, in that case, you could use:

css=input[id^='ember'] (the ^ means starts with)

You can also use endswidth, and contains.

A great write up I found on all the different ways of handling dynamic cases will hopefully help.

3
  • I tried plenty of methods like start with and contains. They did not help out. I dont know whether the problem is i cant use them or not. Mar 30, 2021 at 7:28
  • @KadirÖzdemir note that you'll need to use css selectors, not xpath...
    – Mark Mayo
    Mar 30, 2021 at 8:24
  • I havent used them so i am disoriented a bit. Mar 30, 2021 at 17:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.