1

I'm trying to set dropdowns on this page:

The first dropdown and the fourth dropdown are very similar (brands and country). This is the code I'm using for getting the brand (oem) and country:

oem = Select(wd.find_element_by_css_selector("#alBrandsList"))
oem.select_by_visible_text("Acer")

countries = Select(wd.find_element_by_css_selector("#alCountriesList"))
countries.select_by_visible_text("Albania")

The dropdown is technically hidden, but it somehow seems to work for the device/oem dropdown. For the countries dropdown it is saying that the content isn't visible (which it is). Here's the HTML code it's pulling from:

<select class="pretty-dropdown" datatosent="brand" id="alBrandsList" name="alBrandsList" selectorid="alPhoneModelsList" target="/AdvanceLookup/GetPhoneModels/" style="display: none;">
      ...
</select>
<button type="button" class="ui-multiselect ui-widget ui-state-default ui-corner-all" aria-haspopup="true" style="width: 232px;">
  <span class="ui-icon ui-icon-triangle-2-n-s"></span>
  <span>Please select brand(s)</span>
</button>

<select class="pretty-dropdown" datatosent="country" id="alCountriesList" name="alCountriesList" selectorid="alCarriersList" target="/AdvanceLookup/GetCarriers/" style="display: none;">
      ...
</select>
<button type="button" class="ui-multiselect ui-widget ui-state-default ui-corner-all" aria-haspopup="true" style="width: 232px;">
  <span class="ui-icon ui-icon-triangle-2-n-s"></span>
  <span>Please select country</span>
</button>

Any idea why it works on the first one but not the second one?

Moein Kameli
  • 976
  • 1
  • 12
  • 21
Chelsea
  • 335
  • 2
  • 13

1 Answers1

2

As both the <select> tags for the first and the fourth dropdown on the page https://willmyphonework.net/AdvanceLookup is having the property style="display: none; you can't use Select Class. Instead you need to to induce WebDriverWait for the element_to_be_clickable() and you can use the following Locator Strategies:

  • Code Block:

    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
    driver.get('https://willmyphonework.net/AdvanceLookup')
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='ui-multiselect ui-widget ui-state-default ui-corner-all']//span[text()='Please select brand(s)']"))).click()
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//ul[@class='ui-multiselect-checkboxes ui-helper-reset']//li/label/input[@title='Acer']"))).click()
    driver.find_element_by_xpath("//ul[@class='ui-helper-reset']//li/a/span[@class='ui-icon ui-icon-circle-close']").click()
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='ui-multiselect ui-widget ui-state-default ui-corner-all']//span[text()='Please select country']"))).click()
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//ul[@class='ui-multiselect-checkboxes ui-helper-reset']//li/label/input[@title='Albania']"))).click()
    driver.find_element_by_xpath("//div[@class='ui-multiselect-menu ui-widget ui-widget-content ui-corner-all']//following::ul[6]//a[@class='ui-multiselect-close']/span[@class='ui-icon ui-icon-circle-close']").click()
    
  • Browser Snapshot:

advance_lookup

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Oddly enough this worked for a while but now no longer works for the country dropdown, still works for the Model dropdown. It still got me on the right path and I'm having to use a variation of the XPath now which doesn't lend itself to looping it for multiple countries later down the road. This is my first time using Selenium, usually scrape static sites with BeautifulSoup. This is a whole new challenge. – Chelsea Dec 07 '19 at 18:10