无法将Python Selenium滚动到视图中

时间:2020-04-29 17:02:47

标签: python selenium automation

我正在自动化一些Web进程,但是使用Selenium遇到了一些奇怪的问题。我有:

<div data-field-name="incident_type_ids" style=""><div class="form-group" data-field-name="incident_type_ids">
  <label class="col-xs-4 control-label">
    Incident Type
    <i class="fa fa-info-circle" rel="tooltip" title="" data-placement="right" data-original-title="The type of incident (On Closure please verify that the original type was valid)"></i>
  </label>
  <div class="col-xs-8 controls">

    <span class="editmode">
      <select name="incident_type_ids" multiple="multiple" data-placeholder="Choose Some Types" class="chosen" style="display: none;"><option class=" " value="1007" title="Asset Theft/Loss">
  Asset Theft/Loss


<ul class="chosen-results"><li class="active-result" data-option-array-index="0" title="Dog">
  Dog
</li><li class="active-result  " data-option-array-index="1" title="Cat">
  Cat
</li><li class="active-result  " data-option-array-index="2" title="Mouse">
  Mouse
</li><li class="active-result  " data-option-array-index="3" title="Hunting">
  Hunting
</li><li class="active-result  " data-option-array-index="4" title="Information">
  Information
</li><li class="active-result  " data-option-array-index="5" title="Intelligence">
  Intelligence
</li><li class="active-result  " data-option-array-index="6" title="Request">
  Request
</li><li class="active-result  " data-option-array-index="7" title="Sky">
  Sky
</li><li class="active-result  " data-option-array-index="8" title="Phishing">
  Phishing
</li><li class="active-result  " data-option-array-index="9" title="Violation">
  Violation
</li><li class="active-result  " data-option-array-index="10" title="DDoS">
  DDoS
</li><li class="active-result  " data-option-array-index="11" title="Engineering">
  Engineering
</li><li class="active-result  " data-option-array-index="12" title="Intrusion">
  Intrusion
</li></ul>

如果我单击并使用WebDriverWait选择值

WebDriverWait(driver,30).until(EC.element_to_be_clickable((By.XPATH,"//div[@data-field-name='incident_type_ids']"))).click()
WebDriverWait(driver,30).until(EC.presence_of_element_located((By.XPATH,"//ul[@class='chosen-results']//li[contains(.,'Phishing')]"))).click()

我收到错误消息:

selenium.common.exceptions.ElementNotInteractableException: Message: Element <li class="result-selected"> could not be scrolled into view

这没有任何意义,因为如果我将Phishing替换为下拉菜单中的任何其他值,它将起作用。那么,为什么它只停留在Phising上呢?滚动距离还不远,如果我将其替换为Intrusion,它仍然可以正常工作

1 个答案:

答案 0 :(得分:1)

该错误提示您需要滚动到该元素。可以在硒中实现。

尝试以下选项。

1使用javascript执行器滚动。

WebDriverWait(driver,30).until(EC.element_to_be_clickable((By.XPATH,"//div[@data-field-name='incident_type_ids']"))).click()
element=WebDriverWait(driver,30).until(EC.presence_of_element_located((By.XPATH,"//ul[@class='chosen-results']//li[contains(.,'Phishing')]")))
driver.execute_script("arguments[0].scrollIntoView()", element)
element.click()

2使用硒属性location_once_scrolled_into_view

WebDriverWait(driver,30).until(EC.element_to_be_clickable((By.XPATH,"//div[@data-field-name='incident_type_ids']"))).click()
element=WebDriverWait(driver,30).until(EC.presence_of_element_located((By.XPATH,"//ul[@class='chosen-results']//li[contains(.,'Phishing')]")))
element.location_once_scrolled_into_view
element.click()

如果此时遇到诸如 NOT 之类的错误,请单击JS。

WebDriverWait(driver,30).until(EC.element_to_be_clickable((By.XPATH,"//div[@data-field-name='incident_type_ids']"))).click()
element=WebDriverWait(driver,30).until(EC.presence_of_element_located((By.XPATH,"//ul[@class='chosen-results']//li[contains(.,'Phishing')]")))
element.location_once_scrolled_into_view
driver.execute_script("arguments[0].click();", element)