0

I'm using Selenium to get some info in a webpage, but I need to choose an specific item in a dropdown menu list first. Here it is what the page looks like:

enter image description here

I want to click the "Tracker Availavility" option. I tried getting the class (highlighted in image) and change it's value, but that doesn't work... Any help would be greatly appreciated!

Edit HTML Code:

<select class="ng-valid ng-touched ng-not-empty ng-dirty ng-valid-parse"> style="width: 100px; height: 33px; margin-left: 5px; border-radius: 2px;" ng-model="$ctrl.selectedScratchPad" ng-options="s.name for s in $ctrl.scratchPads track by s.name" ng-change="$ctrl.scratchPadSelected($ctrl.selectedScratchPad)"
<!-- ngIf: $ctrl.selectedScratchPad === null -->
<option label="G&T" value="G&T" selected="selected">G&T</option>
<option label="Relatório semanal" value="Relatório semanal" selected="selected">Relatório semanal</option>
<option label="CBs current" value="CBs current">CBs current</option>
<option label="Tracker Availability" value="Tracker Availability">Tracker Availability</option>
<option label="INV 5-1 Trackers target" value="INV 5-1 Trackers target">INV 5-1 Trackers target</option>
<option label="INV 5-1 Trackers current" value="INV 5-1 Trackers current">INV 5-1 Trackers current</option>
<option label="INV 5-1 Trackers availability" value="INV 5-1 Trackers availability">INV 5-1 Trackers availability</option>
<option label="PVSyst Input" value="PVSyst Input">PVSyst Input</option>

Pedro de Sá
  • 760
  • 6
  • 19
  • Also tried this: `driver.find_element_by_xpath("//select[@class='ng-valid ng-touched ng-not-empty ng-dirty ng-valid-parse']/option[text()='Tracker Availability']").click()` but I get an error saying: "Unable to locate element". – Pedro de Sá Apr 27 '19 at 14:47
  • Add the code and error to the post. Also, Add HTML code instead of the screenshot. – S Ahmed Apr 27 '19 at 14:49
  • @SAhmed sorry for that. Just edited now! – Pedro de Sá Apr 27 '19 at 14:56

2 Answers2

1

Here is the option to click on the list item directly.

driver.find_element_by_xpath("//select[@ng-model='$ctrl.selectedScratchPad']/option[.='Tracker Availability']").click()
supputuri
  • 13,644
  • 2
  • 21
  • 39
  • Thanks supputuri! That's perfect for me! – Pedro de Sá Apr 27 '19 at 15:14
  • Hey man, now I just want to click a button `` after that but I keep getting that the button "could not be scrolled into view". I've changed your code using `WebDriverWait` but couldn't get rid of that... Any ideas? – Pedro de Sá Apr 27 '19 at 15:42
  • Refer to my answer [how to click](https://stackoverflow.com/questions/55876620/how-to-click-custom-html-tag-with-selenium) a element. you have to create element using xpath `//i[@class='fa fa-save']`. I would recommend open a new query when you have a different question, so that people look at the original post does not get confused. – supputuri Apr 27 '19 at 15:51
1

Instead of clicking the selection you can select the option from a <select> element by using the Select class.

Try this:

element = WebDriverWait(driver,10).until(EC.presence_of_all_elements_located((By.XPATH,"//select[@ng-model='$ctrl.selectedScratchPad']")))
dropdown = Select(element)
dropDown.select_by_visible_text("Tracker Availability")

For this, you have to import the following.

from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
S Ahmed
  • 1,454
  • 1
  • 8
  • 14