元素明明存在却无法定位
元素明明存在却无法定位
分析元素的状态:
元素存在:Exist ;
元素可见:Dispalyed ;
元素可用:Enabled ;
元素可点击:Clickable ;
使用findElement.By定位元素显示存在,却无法获取元素Text,可能是由于元素不可见。
Dispalyed()相关伪代码:
ele = driver.findElement.By...;
return ele.is_Dispayed();
使用scrollTo()操作滚动条使元素可见
def ScrollToTarget()
{
int height =0;
while(height<10000)
{
//ClassName为滚动条所在表单的class
js.execute_script("document.getElementByClassNameBy("xxx").ScrollTo(0,height)");
//横向滚动条为"document.getElementByClassNameBy("xxx").ScrollTo(height,0)"
height+=300;
if(driver.findElement.By().is_Dispayed())
{
break;
}
}
}
其他判断元素可见方法:
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get('https://www.baidu.com/')
baidu_input = (By.ID, 'kw')
WebDriverWait(driver,10).until(EC.visibility_of_element_located(baidu_input))
driver.close()

浙公网安备 33010602011771号