Selenium+chromedriver不能仅在某些页面中加载HTML,但不能在其他页面中加载

2024-05-18 23:26:30 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我问的同一个项目here

然而,这次我遇到了另一个问题。基本上,我试图在More information切换链接下获取2个字段UpdatedPublished(为这个切换选择的HTML是"//a[@class='toggle_info_btn']"

在一个页面https://thehive.itch.io/promnesia中,我可以检索这两个字段。但是在另一个页面https://dmullinsgames.itch.io/paper-jekyll中,即使两个页面都有相同的HTML,我也不能。你知道吗

以下是我的代码(正如Yosuva在上一个问题中所建议的):

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
import time

driver = webdriver.Chrome('chromedriver')  # Optional argument, if not specified will search path.
driver.implicitly_wait(15)

driver.get("https://dmullinsgames.itch.io/paper-jekyll");
driver.find_element(By.XPATH,"//a[@class='toggle_info_btn']").click()
time.sleep(2)
WebDriverWait(driver, 3).until(EC.presence_of_element_located((By.XPATH, "//div[@class='game_info_panel_widget']/table//tr//td"))) #Wait for specific element 

table_rows= driver.find_elements(By.XPATH,"//div[@class='game_info_panel_widget']/table//tr//td")

for rows in table_rows:
    print(rows.text)

driver.quit()

运行这个程序时,我看到chromedriver打开了一个Chrome窗口,其中有一个页面,但是我没有看到UpdatedPublished这两个字段。 以下是chromedriver打开Chrome实例时看到的内容: enter image description here

以下是实际上的内容enter image description here 请告诉我这是什么问题。。。你知道吗


Tags: fromhttpsioimportinfobydriverselenium
1条回答
网友
1楼 · 发布于 2024-05-18 23:26:30

正如D.Weltrowski在评论中所回答的,页面中的一些字段只有在登录时才可见。此外,同一字段可以在一个页面上可见,但在另一个页面上不可见。因此,解决方案是在爬网之前让Scrapy登录,这样它就能够抓取这些数据。已验证爬网的信息here

相关问题 更多 >

    热门问题