Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 即使WebElement不可见,_display()方法是否也返回true_Python_Python 3.x_Selenium_Selenium Webdriver_Automated Tests - Fatal编程技术网

Python 即使WebElement不可见,_display()方法是否也返回true

Python 即使WebElement不可见,_display()方法是否也返回true,python,python-3.x,selenium,selenium-webdriver,automated-tests,Python,Python 3.x,Selenium,Selenium Webdriver,Automated Tests,我正在做一个测试,在某个时候,它会记录一些显示在可滚动表格中的数据: <div class='table-body'> <div class='scroll-wrapper> <div class='row'> <button class='button' type='button'></button> <div class='inner-data></div>

我正在做一个测试,在某个时候,它会记录一些显示在可滚动表格中的数据:

<div class='table-body'>
   <div class='scroll-wrapper>
      <div class='row'>
         <button class='button' type='button'></button>
         <div class='inner-data></div>
      </div>
      <div class='row'>
         <button class='button' type='button'></button>
         <div class='inner-data></div>
      </div>
      <div class='row'>
         <button class='button' type='button'></button>
         <div class='inner-data></div>
      </div>
   </div>
</div>
我尝试使用
is_display()
方法和
is_enabled()
检查元素在屏幕上是否可见,不幸的是,它们总是返回True

你们有什么解决办法吗?

此错误消息

ElementNotInteractableException: Message: Element <button class="btn type="button"> could not be scrolled into view
但是对于某些其他元素返回错误
elementnotinteractiableexception

<button class="btn      type="button">
observe the ^^^class^^^ attribute value
  • 使用
    XPATH

    for element in WebDriverWait(driver, 30).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@class='table-body']/div[@class='scroll-wrapper']//div[@class='row']/button[@class='button' and @type='button']"))):
        WebDriverWait(driver, 20).until(EC.visibility_of(element)).click()
    
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

    • 谢谢您的回答@DebanjanB。您的解决方案运行良好:

      try:
          WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS-SELECTOR, "button"))):
          element.click()
      except TimeoutException:
          # The button is not clickable, I scroll down
      
      但是,我知道只有当按钮在DOM中,但在我的窗口中不可见时,它才是不可单击的(并且不可见的)。即使你的解决方案有效,考虑到我在20秒后有一个超时,这也是相当耗时的。我知道我可以在
      WebDriverWait(driver,time)
      解决方案中减少时间参数,但我找到了另一个解决方案:

      try:
          self.driver.find_element_by_css_selector('button').click() 
      except ElementNotInteractableException:
          # The button is not clickable, I scroll down
      

      无论如何,谢谢你的帮助,我希望这个把戏能帮助别人;)

      抱歉,这是帖子中的拼写错误。在我的实际项目中,没有这样的错误。对不起,我编辑了这篇文章来纠正这一点。但问题仍然存在remains@ZoulouDu94签出更新的答案并让我知道状态。这不是评论,而是规范的答案:)
      for element in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "div.table-body > div.scroll-wrapper div.row > button.button[type='button']"))):
          WebDriverWait(driver, 20).until(EC.visibility_of(element)).click()
      
      for element in WebDriverWait(driver, 30).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@class='table-body']/div[@class='scroll-wrapper']//div[@class='row']/button[@class='button' and @type='button']"))):
          WebDriverWait(driver, 20).until(EC.visibility_of(element)).click()
      
      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support import expected_conditions as EC
      
      try:
          WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS-SELECTOR, "button"))):
          element.click()
      except TimeoutException:
          # The button is not clickable, I scroll down
      
      try:
          self.driver.find_element_by_css_selector('button').click() 
      except ElementNotInteractableException:
          # The button is not clickable, I scroll down