如何通过Selenium和Python从没有Select标签的Dropdown中选择最后一个值

问题描述 投票:-1回答:2

我是使用Selenium自动化的新手。我试图选择下拉列表中的最后一个元素,该元素显示yahoo财经网页上页面上的(库存)行数。当我检查HTML时,我没有看到任何选择标签,因此我无法在下拉列表中搜索选项。此外,我不确定如何在单击向下箭头后搜索XPATH或元素,因为它不在页面源中。

以下是我正在处理的代码。我可以点击下拉菜单,但我仍然坚持下一步,点击最后一个选项。

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()

我能够提取的HTML片段如下:

<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>

我感谢您的帮助。先感谢您。

python selenium selenium-webdriver dropdown webdriverwait
2个回答
0
投票

要选择下拉列表中显示finance.yahoo网页上页面(库存)行数的最后一个元素,您可以使用以下解决方案:

  • 代码块: 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()
  • 浏览器快照:

count100


0
投票

如果要遍历所有元素,请不要单击。然后

获取以下文字。它显示它包含466个结果。

401-466的466结果

所以你的第一个网址就是

?offset=0&count=100

第二个是

?offset=100&count=100

第三

?offset=200&count=100

第四

?offset=300&count=100

最后

?offset=400&count=100

你可以做数学。

但是,如果要单击下拉选项,则。

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()

在此之后,添加了新的div元素,其中包含span供您选择。您共享的HTML代码,它没有关联。

<div class="Pt(5px) Pb(5px) Pos(a) End(0px) Z(10) Bgc(#fff) Bd Bdc($c-fuji-grey-c) Bdrs(2px) Bxsh($boxShadow) Whs(nw)" data-test="showRows-select-menu">
    <div class="Ta(start) Px(20px) Py(10px) Cur(p) Bgc($pillBgBlue):h Fz(s)" data-value="25">
        <span>Show 25 rows</span>
    </div>
    <div class="Ta(start) Px(20px) Py(10px) Cur(p) Bgc($pillBgBlue):h Fz(s)" data-value="50">
        <span>Show 50 rows</span>
    </div>

    <div class="Ta(start) Px(20px) Py(10px) Cur(p) Bgc($pillBgBlue):h Fz(s) Fw(b)" data-value="100">
    <span>Show 100 rows</span>
    </div>
</div>

所以你需要做的是,点击link之后你需要等到这个元素被添加到你的html中。

xpath = "//div[@data-test='showRows-select-menu']"
elem = wait.until(EC. presence_of_element_located((By.XPATH, xpath)))
total_dropdown = len(driver.find_elements_by_xpath("//div[@data-test='showRows-select-menu']/div")
driver.find_elements_by_xpath("//div[@data-test='showRows-select-menu']/div[total_dropdown+1]/span").click()

我没有测试过代码,但我认为你有了这个想法。

© www.soinside.com 2019 - 2024. All rights reserved.