我正在尝试从网站获取信息,但失败了。
由于未知原因,结果(案例)为空。有什么线索吗?
以下是我的代码摘录:
if __name__ == '__main__':
driverPath = "D:\In\Python\selenium\chromedriver.exe"
driver = webdriver.Chrome(driverPath)
url = "https://www.hktutorcentre.com/"
driver.get(url)
try:
listCases = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "list_cases"))
)
#search for cases
cases = listCases.find_elements_by_class_name('layui-col-lg2')
for case in cases:
print(case)
finally:
driver.quit()发布于 2021-04-15 01:18:41
您的代码中至少存在树问题:
1
listCases = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "list_cases"))即使list_cases是ID元素,它仍然不是唯一的
2 find_elements_by_class_name('layui-col-lg2')这里你要找的不是文本,它只是WebElement。
3使用visibility_of_element_located而不是presence_of_element_located
4如果您想从元素中获取文本,例如标题,请使用:
driver.find_element_by_css_selector(".layui-col-lg2>.box>.content.blue>h1").texthttps://stackoverflow.com/questions/67095765
复制相似问题