Search code examples
pythonseleniumselenium-webdriverdropdownwebdriverwait

How to select the last value from the Dropdown without Select tag through Selenium and Python


I am new to automating using Selenium. I was trying to select the last element in the drop-down that shows the number of (stock) rows on the page on yahoo finance web-page. When I inspect the HTML I do not see any select tags and hence I cannot search for options in the dropdown. Also I am not sure how to search for the XPATH or elements after clicking the down arrow as it is not present in the page source.

Below is the code that I am working on. I am able to click the dropdown but am stuck on the next steps to click on the last option.

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


webDriver = webdriver.Chrome()
wait = WebDriverWait(webDriver, 10)

webDriver.maximize_window()
webDriver.get("https://finance.yahoo.com/sector/technology")

xpath = "//div[@data-test='select-container']"
wait.until(EC.visibility_of_element_located((By.XPATH, xpath)))
wait.until(EC.element_to_be_clickable((By.XPATH, xpath)))
webDriver.find_element_by_xpath(xpath).click()

The HTML snippet that I was able to extract is as follows:

<div class="W(100%) Mt(15px) Ta(end)" data-reactid="1375">
   <span class="Pos(r) Mend(20px) Va(m)" data-reactid="1376">
      <div class="O(n):f O(n):h P(0) M(0) Cur(p):h D(ib)" tabindex="0" data-test="select-container" data-reactid="1377">
         <span data-test="showRows-select-selected" class="O(n):f O(n):h P(0) M(0) C($c-fuji-blue-1-b) Fz(s) Fw(500)" data-reactid="1378"><span data-reactid="1379">Show 50 rows</span></span>
         <svg class="H(8px) W(8px) Va(m)! Mstart(8px) Stk($c-fuji-blue-1-b)! Fill($c-fuji-blue-1-b)! Cur(p)" width="8" style="fill:#000;stroke:#000;stroke-width:0;vertical-align:bottom;" height="8" viewBox="0 0 512 512" data-icon="CoreArrowDown" data-reactid="1380">
            <path d="M500.77 131.432L477.53 108.18c-14.45-14.55-40.11-14.55-54.51 0L255.845 275.363 88.582 108.124c-15.015-14.874-39.363-14.874-54.42.108L10.94 131.486c-14.58 14.44-14.58 40.11-.033 54.442l217.77 217.845c15.004 14.82 39.33 14.874 54.42-.108L500.88 185.82c14.818-14.982 14.87-39.298-.11-54.388z" data-reactid="1381"></path>
         </svg>
      </div>
   </span>
   <button class="Va(m) Bd(0) M(0) P(0) Mend(10px) O(n):f C($gray)" disabled="" data-reactid="1382">
      <svg class="Va(m)! Fill($gray)! Stk($gray)! Cur(a)! Cur(p)" width="18" style="fill:#000;stroke:#000;stroke-width:0;vertical-align:bottom;" height="18" viewBox="0 0 48 48" data-icon="double-left" data-reactid="1383">
         <path d="M14.605 23.995l9.813-9.813c.755-.757.767-2.023-.006-2.795-.78-.78-2.027-.777-2.795-.006L9 23.996l12.62 12.62c.757.756 2.023.767 2.796-.007.78-.78.777-2.025.006-2.796l-9.817-9.817zM26.605 23.995l9.813-9.813c.755-.757.767-2.023-.006-2.795-.78-.78-2.027-.777-2.795-.006L21 23.996l12.62 12.62c.757.756 2.023.767 2.796-.007.78-.78.777-2.025.006-2.796l-9.817-9.817z" data-reactid="1384"></path>
      </svg>
   </button>

I appreciate your help. Thank you in advance.


Solution

  • To select the last element in the drop-down that shows the number of (stock) rows on the page on finance.yahoo web-page you can use the following solution:

    • 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
      
      options = webdriver.ChromeOptions()
      options.add_argument("start-maximized")
      options.add_argument("disable-infobars")
      options.add_argument("--disable-extensions")
      webDriver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
      webDriver.get("https://finance.yahoo.com/sector/technology")
      WebDriverWait(webDriver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span[data-test='showRows-select-selected']"))).click()
      WebDriverWait(webDriver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[@data-test='showRows-select-menu']//*[contains(., 'Show 100 rows')]"))).click()
      
    • Browser Snapshot:

    count100