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