Python Selenium 下拉菜单最后一个元素选择
Python Selenium Dropdown Menu Last Element Selection
我正在尝试 select 默认菜单中未显示的元素。该元素是最后一个,除非向下滚动,否则不会显示。我可以更改元素编号,但它不会更新元素编号的 table 值。下面是我试过的代码。任何指针将不胜感激!查看了一些问题,无法复制任何有效的解决方案。
截屏:
Page Source Image
代码如下:
week_dropdown = driver.find_element_by_xpath('//*[@id="SEARCH"]/div/div[3]/div[2]/div[1]/div/div')
driver.execute_script("arguments[0].click();", week_dropdown)
page_value_span = driver.find_element_by_xpath('//*[@id="SEARCH"]/div/div[3]/div[2]/div[1]/div/div/span[2]')
page_value_edit = driver.execute_script('arguments[0].innerHTML = "1";', page_value_span)
driver.execute_script("arguments[0].click();",page_value_span)
<div span="8" class="ant-row ant-row-end" style="padding-top: 10px;"><div class="style_reportOptionBlock__1Qm2T" style="margin-right: 20px;"><label class="style_vlabelfull__1uP_p">#Weeks: </label><div class="ant-select style_vSelectSearchnew__3UstT ant-select-single ant-select-show-arrow" style="width: 60px;"><div class="ant-select-selector"><span class="ant-select-selection-search"><input autocomplete="off" class="ant-select-selection-search-input" role="combobox" aria-haspopup="listbox" aria-owns="rc_select_1_list" aria-autocomplete="list" aria-controls="rc_select_1_list" aria-activedescendant="rc_select_1_list_-1" readonly="" unselectable="on" value="" id="rc_select_1" style="opacity: 0;" aria-expanded="false"></span><span class="ant-select-selection-item" title="4">4</span></div><span class="ant-select-arrow" unselectable="on" aria-hidden="true" style="user-select: none;"><span role="img" aria-label="down" class="anticon anticon-down ant-select-suffix"><svg viewBox="64 64 896 896" focusable="false" class="" data-icon="down" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M884 256h-75c-5.1 0-9.9 2.5-12.9 6.6L512 654.2 227.9 262.6c-3-4.1-7.8-6.6-12.9-6.6h-75c-6.5 0-10.3 7.4-6.5 12.7l352.6 486.1c12.8 17.6 39 17.6 51.7 0l352.6-486.1c3.9-5.3.1-12.7-6.4-12.7z"></path></svg></span></span></div></div>
如果您可以滚动浏览菜单,请在 clicking/selecting 最后一个选项之前先稍等片刻。让我知道这是否有效?
您可以尝试 ActionChains
week_dropdown = driver.find_element_by_xpath("xpath for open dropdown...").click()
element = driver.find_element_by_xpath("//div[@class='ant-select-item ant-select-item-option']/div[text()='1']")
actions = ActionChains(driver)
actions.move_to_element(to_element=element)
actions.click(on_element=element).perform()
确保导入 ActionChains
from selenium.webdriver.common.action_chains import ActionChains
您也可以尝试 arguments[0].scrollIntoView()
week_dropdown = driver.find_element_by_xpath("xpath for open dropdown...").click()
element = driver.find_element_by_xpath("//div[@class='ant-select-item ant-select-item-option']/div[text()='1']")
driver.execute_script('arguments[0].scrollIntoView();', element)
这是您要找的吗?
我正在尝试 select 默认菜单中未显示的元素。该元素是最后一个,除非向下滚动,否则不会显示。我可以更改元素编号,但它不会更新元素编号的 table 值。下面是我试过的代码。任何指针将不胜感激!查看了一些问题,无法复制任何有效的解决方案。
截屏:
Page Source Image
代码如下:
week_dropdown = driver.find_element_by_xpath('//*[@id="SEARCH"]/div/div[3]/div[2]/div[1]/div/div')
driver.execute_script("arguments[0].click();", week_dropdown)
page_value_span = driver.find_element_by_xpath('//*[@id="SEARCH"]/div/div[3]/div[2]/div[1]/div/div/span[2]')
page_value_edit = driver.execute_script('arguments[0].innerHTML = "1";', page_value_span)
driver.execute_script("arguments[0].click();",page_value_span)
<div span="8" class="ant-row ant-row-end" style="padding-top: 10px;"><div class="style_reportOptionBlock__1Qm2T" style="margin-right: 20px;"><label class="style_vlabelfull__1uP_p">#Weeks: </label><div class="ant-select style_vSelectSearchnew__3UstT ant-select-single ant-select-show-arrow" style="width: 60px;"><div class="ant-select-selector"><span class="ant-select-selection-search"><input autocomplete="off" class="ant-select-selection-search-input" role="combobox" aria-haspopup="listbox" aria-owns="rc_select_1_list" aria-autocomplete="list" aria-controls="rc_select_1_list" aria-activedescendant="rc_select_1_list_-1" readonly="" unselectable="on" value="" id="rc_select_1" style="opacity: 0;" aria-expanded="false"></span><span class="ant-select-selection-item" title="4">4</span></div><span class="ant-select-arrow" unselectable="on" aria-hidden="true" style="user-select: none;"><span role="img" aria-label="down" class="anticon anticon-down ant-select-suffix"><svg viewBox="64 64 896 896" focusable="false" class="" data-icon="down" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M884 256h-75c-5.1 0-9.9 2.5-12.9 6.6L512 654.2 227.9 262.6c-3-4.1-7.8-6.6-12.9-6.6h-75c-6.5 0-10.3 7.4-6.5 12.7l352.6 486.1c12.8 17.6 39 17.6 51.7 0l352.6-486.1c3.9-5.3.1-12.7-6.4-12.7z"></path></svg></span></span></div></div>
如果您可以滚动浏览菜单,请在 clicking/selecting 最后一个选项之前先稍等片刻。让我知道这是否有效?
您可以尝试 ActionChains
week_dropdown = driver.find_element_by_xpath("xpath for open dropdown...").click()
element = driver.find_element_by_xpath("//div[@class='ant-select-item ant-select-item-option']/div[text()='1']")
actions = ActionChains(driver)
actions.move_to_element(to_element=element)
actions.click(on_element=element).perform()
确保导入 ActionChains
from selenium.webdriver.common.action_chains import ActionChains
您也可以尝试 arguments[0].scrollIntoView()
week_dropdown = driver.find_element_by_xpath("xpath for open dropdown...").click()
element = driver.find_element_by_xpath("//div[@class='ant-select-item ant-select-item-option']/div[text()='1']")
driver.execute_script('arguments[0].scrollIntoView();', element)
这是您要找的吗?