javascript - Selenium if driver.is_enabled(): 不起作用
问题描述
大家好,我有一个任务是从这个链接做一些抓取工作,问题是我正在尝试通过单击来进入页面的最终外观,这是带有一些数据的表格,但有时它只需要单击一下,然后在另一方面,它需要不止一个。在这种情况下,我尝试通过使用 if 语句进行控制,其中我说过如果页面最终视图中的元素is_enabled()(我也尝试过is_displayed())转到打印出最终页面视图的函数。请如果你能帮助我,因为我不知道如何解决这个问题了。这是我的代码
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import StaleElementReferenceException
from selenium.common.exceptions import ElementNotInteractableException
from bs4 import BeautifulSoup
import time
import urllib.request
import re
ignored_exceptions=(NoSuchElementException,StaleElementReferenceException)
#Function that will do writing because I don't have these atrb in final page look as data
def write(make,model,body,nesto):
source = driver.page_source
soup = BeautifulSoup(source, "lxml")
table = soup.find('table')
table_rows = table.find_all('tr')
for tr in table_rows:
td = tr.find_all('td')
scrape_list = [make,model,body,td]
print(scrape_list)
def not_normal():
another_back = driver.find_element_by_class_name("btn btn-back pull-left kba_step_back")
another_back.click()
item_type_list = driver.find_element_by_class_name("kba_vehicle_type_group_id")
item = item_type_list.find_element_by_tag_name("li")
make = driver.find_element_by_class_name("make")
model = driver.find_element_by_class_name("model")
body = driver.find_element_by_class_name("body")
item.click()
another_back.click()
write(make.text,model.text,body.text,item.text)
while True:
try:
button_back = wait.until(EC.element_to_be_clickable((By.ID, "back")))
button_back.click()
except TimeoutException:
break
driver = webdriver.Chrome("/home/cutie/Desktop/felgenoutlet/bin/chromedriver")
driver.get("https://www.felgenoutlet.com/en")
wait = WebDriverWait(driver, 3)
#Opens webpage
first_list = driver.find_element_by_class_name("kba_vehicle_trade_group_id")
wait.until(EC.presence_of_all_elements_located((By.TAG_NAME,'li')))
cars = first_list.find_elements_by_tag_name("li")
for car in cars:
car.click()
try:
final_view=driver.find_element_by_class_name("header_entry_list")
if final_view.is_enabled():
not_normal()
continue
except:
print("not found", end='\n')
second_list = driver.find_element_by_class_name("kba_vehicle_model_group_id")
second_list_items= wait.until(EC.presence_of_all_elements_located((By.TAG_NAME,'li')))
second_list_items = second_list.find_elements_by_tag_name("li")
for model in second_list_items:
model.click()
try:
final_view=driver.find_element_by_class_name("header_entry_list")
if final_view.is_enabled():
not_normal()
continue
except:
third_list = driver.find_element_by_class_name("kba_vehicle_body_group_id")
third_list_items= wait.until(EC.presence_of_all_elements_located((By.TAG_NAME,'li')))
third_list_items = third_list.find_elements_by_tag_name("li")
for body in third_list_items:
body.click()
try:
final_view=driver.find_element_by_class_name("header_entry_list")
if final_view.is_enabled():
not_normal()
continue
except:
fourth_list = driver.find_element_by_class_name("kba_vehicle_type_group_id")
fourth_list_items = wait.until(EC.presence_of_all_elements_located((By.TAG_NAME,'li')))
fourth_list_items = fourth_list.find_elements_by_tag_name("li")
for nesto in fourth_list_items:
make = driver.find_element_by_class_name("make").text
model = driver.find_element_by_class_name("model").text
body = driver.find_element_by_class_name("body").text
write(make,model,body,nesto.text)
nesto.click()
这是我有问题的 if 语句:
final_view=driver.find_element_by_class_name("header_entry_list")
if final_view.is_enabled():
not_normal()
continue
任何帮助将不胜感激 更新问题解决很快代码解决方案将被上传
解决方案
selenium 的 .isEnabled() 方法仅检查元素上的禁用或启用标记。如果元素没有禁用或启用标签(即 div disabled="disabled"/div),那么 isEnabled() 将不适用于该元素。
看起来您使用类名“header_entry_list”定位的元素是一个表元素,它没有禁用/启用标签。
isDisplayed() 检查宽度和高度是否大于 0 我相信,无论元素在视口中是否可见(例如在屏幕上,向下滚动),它都会返回 true。
也许您可以检查您希望在单击适量尝试时出现的元素并使用布尔值来处理重复单击。
推荐阅读
- reactjs - S3 前端向 EC2 显示 CORS 预检错误
- javascript - 有没有办法从可选变量中解包?
- python - 将遗留系统与 Celery 集成
- full-text-search - 两个数据库的快速全文比较
- javascript - 创建频道后找不到频道 discord.js v13
- javascript - Buddypress:还保存个人资料图片的大版本
- bash - bash 回显到文件失败并显示“没有这样的文件”
- python - Python BeautifulSoup 类查找返回无
- php - 点转移到用户错误 laravel
- python - 将 exog 变量添加到 SARIMAX 网格搜索中