python - Selenium - 无法在页面源中找到元素
问题描述
我正在尝试使用 Selenium 抓取网页,但由于某种原因,我需要的元素未显示在页面源中
我尝试使用 WebDriverWait 直到页面加载。我还尝试查看数据是否在我需要切换到的不同帧中。
driver.get('https://foreclosures.cabarruscounty.us/')
try:
WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.XPATH,'//*[@id="app"]/div[5]/div/div')))
print("Page is ready!")
web_url = driver.page_source
print(web_url)
except TimeoutException:
print("Loading took too much time!")
我希望看到我可以提取的每个单独的财产卡的所有记录。但是,页面源不显示任何此类数据。
如果我手动加载网页并检查源,数据只是不存在查看源:https ://foreclosures.cabarruscounty.us/
解决方案
要提取第一个Real ID、Case Number和Owner 字段,您必须诱导WebDriverWait并且visibility_of_element_located()
您可以使用以下Locator Strategies:
代码块:
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC chrome_options = webdriver.ChromeOptions() chrome_options.add_argument("start-maximized") chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"]) chrome_options.add_experimental_option('useAutomationExtension', False) driver = webdriver.Chrome(options=chrome_options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe') driver.get("https://foreclosures.cabarruscounty.us/"); Real_ID = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='row']//div[@class='card cardClass']/img//following::div[@class='card-body']//div/b"))).text Case_Number = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='row']//div[@class='card cardClass']/img//following::div[@class='card-body']//div//following-sibling::b[2]"))).text Owner = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='row']//div[@class='card cardClass']/img//following::div[@class='card-body']//div//following-sibling::b[7]"))).text print("{} is {} owned by {}".format(Real_ID,Case_Number,Owner)) driver.quit()
控制台输出:
Real ID: 04-086 -0040.00 is Case Number: 18-CVD-2804 owned by Owner: DOUGLAS JAMES W