如何通过 Selenium 和 Python select 没有 Select 标签的下拉列表中的最后一个值
How to select the last value from the Dropdown without Select tag through Selenium and Python
我不熟悉使用 Selenium 进行自动化。
我试图 select 下拉列表中的最后一个元素,该元素显示雅虎财经网页上页面上的(股票)行数。
当我检查 HTML 时,我没有看到任何 select 标签,因此我无法在下拉列表中搜索选项。此外,我不确定如何在单击向下箭头后搜索 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>
感谢您的帮助。先感谢您。
不点击,如果要遍历所有元素。然后
获取以下文本。它显示它包含 466 个结果。
401-466 of 466 results
所以你的第一个 url 将是
?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()
我还没有测试代码,但是我想你明白了。
对于 select 显示 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()
浏览器快照:
我不熟悉使用 Selenium 进行自动化。 我试图 select 下拉列表中的最后一个元素,该元素显示雅虎财经网页上页面上的(股票)行数。 当我检查 HTML 时,我没有看到任何 select 标签,因此我无法在下拉列表中搜索选项。此外,我不确定如何在单击向下箭头后搜索 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>
感谢您的帮助。先感谢您。
不点击,如果要遍历所有元素。然后
获取以下文本。它显示它包含 466 个结果。
401-466 of 466 results
所以你的第一个 url 将是
?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()
我还没有测试代码,但是我想你明白了。
对于 select 显示 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()
浏览器快照: