0

I have a python selenium script that runs perfectly on the windows. The script reads the xpath of the dropdown element and clicks it, displaying the values in the list. When I run the same code on Linux, script click the dropdown but the list values are not displayed. Infact I'm using the same chromedriver version at both the places. I cannot find the element by "ID" because it is dynamically generated by javascript. What could be issue and it's resolution?

  • Inspect screen

enter image description here

  • Xpath user: (//div[text()='Resource Criteria']//parent::div//parent::div//parent::div/div2/div/div/div/div/div/div/div)1

  • Screenshot of the dropdown from windows enter image description here

  • 1
    Some suggestions: 1. Avoid XPath and try to use CSS Selectors; 2. Provide some code; 3. To drive a select element from Selenium, you can use [`Select`](https://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.support.select) – crissal Jun 19 '21 at 20:34
  • @crissal Thanks for suggestion 3 options. I cannot use the CSS Selectors for the same reason as ID. CSS Selector is dynamic. Sometimes it is #s2id_autogen15 and other times it is #s2id_autogen28 based on other fields. Code is built by some other party so can't modify the code. Dropdown is not using SELECT so can't leverage that too. – patna_programmer Jun 19 '21 at 20:54
  • When I told you about CSS selector I meant something like `div > div > ...`, because id was likely to be autogenerated; by the way, about the code, I meant your code/script to show how you use this object – crissal Jun 19 '21 at 21:30
  • @crissal why did you suggest the `1.` option ? I have heard from multiple ppl and also I have experienced that Xpath is a way better than `CSS Selectors` ... if you create your own selectors (for example here `contains(@class, "select2-container")` ) it a way better to move around. Sure, ID might be autogenerated, but you need to create a more robust selecter, the CSS Selector will not be enough. (If you could give a good examples, I will be grateful, thanks in advance) – StyleZ Jun 20 '21 at 01:49
  • I always heard the opposite thing - you can read more [here](https://stackoverflow.com/a/16794683/11647025). Furthermore, Xpath can be clunky if the page change just a little bit; I would search for the element class, that seems to be unique. – crissal Jun 20 '21 at 05:08
  • @crissal Thanks a lot! I used CSS selector to fix my issue based on your suggestion. Another suggestion was to use javascript executor. – patna_programmer Jun 20 '21 at 13:04

1 Answers1

0

Closing this thread as I got it working based on @crissal suggestion. I have used CSS Selector instead of xpath.

  • 1
    Please, either make a summary of the solution as an answer or add this "answer" as a comment and delete it – cards Aug 25 '21 at 20:52