gpt4 book ai didi

Python selenium 获取页面标题

转载 作者:行者123 更新时间:2023-12-03 22:29:14 25 4
gpt4 key购买 nike

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
from selenium.webdriver.firefox.options import Options

options = Options()
options.headless = True
driver = webdriver.Firefox(options=options)
driver.get("https://hapondo.qa/rent/doha/apartments/studio")
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "/html/head/title"))
)

print(element.text)
在 headless 选项下无法获取页面标题?试图等待甚至尝试 driver.title

最佳答案

您需要注意以下几点:

  • 检索页面标题而不是使用 您需要使用 driver.title
  • hapondo网站包含 JavaScript启用的元素。

  • 解决方案
    要提取页面标题,您需要诱导 WebDriverWait对于 title_contains()您可以使用以下任一 Locator Strategy :
  • 代码块:
    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC

    options = webdriver.ChromeOptions()
    options.add_argument('--headless')
    options.add_argument('--window-size=1920,1080')
    driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
    driver.get('https://hapondo.qa/rent/doha/apartments/studio')
    WebDriverWait(driver, 10).until(EC.title_contains("hapondo"))
    print(driver.title)
  • 控制台输出:
    Studio Apartments for rent in Doha | hapondo

  • 引用
    您可以在以下位置找到一些相关的详细讨论:
  • How to make selenium wait before getting contents from the actual website which loads after the landing page through IEDriverServer and IE
  • 关于Python selenium 获取页面标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64793581/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com