-1

I am working on web scraping from LinkedIn. I have a filter for roles on a sales navigator page. Here I need to add filter for roles using python.

driver.find_element(By.XPATH,"//[@id='ember219']/div/input").send_keys('Marketing')

However, the xpath keeps changing each time I open the page like this:

//[@id='ember220']/div/input
//[@id='ember221']/div/input
//[@id='ember222']/div/input

Please help me match the pattern for my driver so that each time I have a smooth action happening. Roles can be Marketing , VP, Director so on.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Sushmitha
  • 111
  • 5

1 Answers1

0

The id attribute values like ember220, ember221 are dynamically generated by Ember.js and is bound to change sooner/later. They may change next time you access the application afresh or even while next application startup. So can't be used in locators as it is.


Solution

As the xpath keeps on changing you can construct Dynamic Locator Strategies and you can use either of the following locator strategies:

  • Using XPATH and starts-with(@id, 'static_id_part'):

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

    Effectively:

    driver.find_element(By.XPATH,"//*[starts-with(@id, 'ember')]/div/input").send_keys('Marketing')
    
  • Using XPATH and contains(@id, 'static_id_part'):

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

    Effectively:

    driver.find_element(By.XPATH,"//*[contains(@id, 'ember')]/div/input").send_keys('Marketing')
    
  • Using CSS_SELECTOR and starts-with(^):

    [id^='ember'] > div > input
    

    Effectively:

    driver.find_element(By.CSS_SELECTOR,"[id^='ember'] > div > input").send_keys('Marketing')
    
  • Using CSS_SELECTOR and contains(*):

    [id*='ember'] > div > input
    

    Effectively:

    driver.find_element(By.CSS_SELECTOR,"[id*='ember'] > div > input").send_keys('Marketing')
    

References

You can find a couple of relevant detailed discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352