我正试图用Python Splinter点击选择下拉列表中的某个元素。
源代码(示例-原始代码中有更多选项值(是:
<div class="row form-group">
<div class="col s12 m12 l6">
<label>
Zeitzone der Website
</label>
<div class="select-wrapper">
<span class="caret">▼</span><input type="text" class="select-dropdown" readonly="true" data-activates="select-options-0388a2bc-2b5c-c534-8870-fc41b27e78c4" value="Wählen Sie eine Stadt">
<ul id="select-options-0388a2bc-2b5c-c534-8870-fc41b27e78c4" class="dropdown-content select-dropdown ">
<li class=""><span>Wählen Sie eine Stadt</span></li>
<li class="optgroup-option "><span>Berlin</span></li>
</ul>
<select name="timezone" id="timezone-0" class="initialized">
<option value="No timezone">Wählen Sie eine Stadt</option>
<optgroup label="Europe">
<option value="Europe/Berlin">Berlin</option>
</optgroup>
</select>
</div>
</div>
</div>
我想选择柏林。
我尝试了browser.find_by_css/text等,但元素不可见,我成为了一个例外。
Traceback (most recent call last):
File "matomo.py", line 598, in <module>
browser.find_option_by_text('Berlin').first.click()
File "/usr/lib/python3.6/site-packages/splinter/driver/webdriver/__init__.py", line 562, in click
self._element.click()
File "/usr/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py", line 80, in click
self._execute(Command.CLICK_ELEMENT)
File "/usr/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py", line 628, in _execute
return self._parent.execute(command, params)
File "/usr/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 320, in execute
self.error_handler.check_response(response)
File "/usr/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: element not visible: Element is not currently visible and may not be manipulated
(Session info: chrome=67.0.3396.99)
(Driver info: chromedriver=2.40.565383 (76257d1ab79276b2d53ee976b2c3e3b9f335cde7),platform=Linux 4.14.56-1-MANJARO x86_64)
有什么想法、解决方法或技巧吗?:(
您可以尝试使用Explicit wait等待,
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable(By.XPATH, "//span[contains(text(),'Berlin')]")).click()
Javascript函数的点击事件:
driver.execute_script("arguments[0].click()", ElementToClick)