繁体   English   中英

如何在隐藏的 class 中通过 pythom selenium 选择 select 选项?

[英]How to select an option via pythom selenium in hidden class?

我正在使用 Selenium 解析网站。

我需要 select 隐藏 class 中的一个选项。 Html这部分代码:

 <div class="search region"> <div class="input-group"> <div class="cec-select"> <select tabindex="-1" class="select2-hidden-accessible" aria-hidden="true"><option></option><option value="value_1">text_1</option><option value="value_2">text_2</option></select> <script>$(function () { $('.cec-select select').select2({ placeholder: "Выберите субъект", "language": { "noMatches": function () { return "Select a subject"; } } }).change(function () { window.open($(this).val(), '_blank'); }); });</script> </div> <br> </div> </div>

我尝试像往常一样通过 Select function 选择 select 选项:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains
from bs4 import BeautifulSoup
from selenium.webdriver.support.ui import Select

driver = webdriver.Chrome(executable_path='./chromedriver', chrome_options=chrome_options)

driver.get('url')

regions = Select(driver.find_element_by_class_name("select2-hidden-accessible"))
regions.select_by_index('2')

我收到此错误消息: ElementNotInteractableException:消息:元素不可交互:元素当前不可见,可能无法操作。

完整的错误信息:

ElementNotInteractableException           Traceback (most recent call last)
<ipython-input-28-7e06e646520e> in <module>
----> 1 regions.select_by_index('2')
~\Anaconda3\lib\site-packages\selenium\webdriver\support\select.py in select_by_index(self, index)
     99         for opt in self.options:
    100             if opt.get_attribute("index") == match:
--> 101                 self._setSelected(opt)
    102                 return
    103         raise NoSuchElementException("Could not locate element with index %d" % index)
~\Anaconda3\lib\site-packages\selenium\webdriver\support\select.py in _setSelected(self, option)
    210     def _setSelected(self, option):
    211         if not option.is_selected():
--> 212             option.click()
    213 
    214     def _unsetSelected(self, option):
~\Anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py in click(self)
     78     def click(self):
     79         """Clicks the element."""
---> 80         self._execute(Command.CLICK_ELEMENT)
     81 
     82     def submit(self):
~\Anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py in _execute(self, command, params)
    631             params = {}
    632         params['id'] = self._id
--> 633         return self._parent.execute(command, params)
    634 
    635     def find_element(self, by=By.ID, value=None):
~\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py in execute(self, driver_command, params)
    319         response = self.command_executor.execute(driver_command, params)
    320         if response:
--> 321             self.error_handler.check_response(response)
    322             response['value'] = self._unwrap_value(
    323                 response.get('value', None))
~\Anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py in check_response(self, response)
    240                 alert_text = value['alert'].get('text')
    241             raise exception_class(message, screen, stacktrace, alert_text)
--> 242         raise exception_class(message, screen, stacktrace)
    243 
    244     def _value_or_default(self, obj, key, default):
ElementNotInteractableException: Message: element not interactable: Element is not currently visible and may not be manipulated

我想,我应该先在选项下运行一个脚本,但我不知道该怎么做。

你可以试试这个方法。 findElementsByXpathfindElementByXpath将需要实现。 请注意,此代码在 Java 中。

public void selectFromDropdown(){

// Find the size of dropdown list items.

List <WebElement> dropdownSize = findElementsByXpath(".//select[@class='select2-hidden-accessible']//option");

// Iterate thru each dropdown value and click each of them.

if(dropdownSize.size() > 0){
    for(int i=1; i<= dropdownSize.size(); i++){
        findElementByXpath(".//select[@class='select2-hidden-accessible']")
        findElementByXpath(".//select[@class='select2-hidden-accessible']//option["+i+"]").click();
        }
    }
}

您可以通过添加 try/catch 来进一步改进它,并在从下拉列表中选择特定值后跳出循环。

解决了这个问题。 首先,我单击文本字段,因此所有选项都在引用上可见。

driver.find_elements_by_xpath("//*[contains(text(), 'Выберите субъект')]")[2].click()

点击前 点击

在 html 代码选项仍然隐藏在 class 中,我仍然无法 select 它们。 但是我注意到在单击文本字段后出现了一些新的 html 代码。 这个新片段复制了我需要的所有选项。 它们现在位于 li 标签中。

 <iframe aria-hidden="true" frameborder="0" name="ym-native-frame" style="opacity: 0;important: width; 0px:important; height: 0px;important: position; absolute:important; left: 100%;important: bottom; 100%:important; border: 0px;important:" title="ym-native-frame"></iframe><ym-measure class="ym-viewport" style="display; block: top; 0px: right; 0px: bottom; 0px: left; 0px: height, 100vh; width: 100vw; position: fixed; transform: translate(0px; -100%): transform-origin; 0px 0px:"></ym-measure><ym-measure class="ym-zoom" style="bottom; 100%: position. fixed; width: 100vw."></ym-measure><span class="select2-container select2-container--default select2-container--open" style="position; absolute: top; 1416.33px; left: 844.99px;"><span class="select2-dropdown select2-dropdown--below" dir="ltr" style="width: 350px;"><span class="select2-search select2-search--dropdown"><input autocapitalize="off" autocomplete="off" autocorrect="off" class="select2-search__field" role="textbox" spellcheck="false" tabindex="0" type="search"/></span><span class="select2-results"><ul aria-expanded="true" aria-hidden="false" class="select2-results__options" id="select2-42bc-results" role="tree"><li aria-selected="false" class="select2-results__option select2-results__option--highlighted" id="id_1" role="treeitem">text_1</li><li aria-selected="false" class="select2-results__option" id="id_2" role="treeitem">text_2</li></ul></span></span></span></body></html>

因此,我使用以下代码访问了 li 元素:

driver.find_elements_by_class_name("select2-results__option")[id].click()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM