Python selenium 获取页面标题

Python selenium get page title

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)

无法在无头选项下获取页面标题?试图等待甚至尝试 driver.title

我假设您所说的“页面标题”是指显示在浏览器顶部选项卡上的文本。

几乎不改变代码的解决方案:

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


driver = webdriver.Firefox(executable_path=r"[path]")
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.get_attribute("innerHTML"))
Output: Studio Apartments for rent in Doha | hapondo

获取该文本的另一种方法是简单地使用 driver.title.

"title 方法用于检索用户当前正在处理的网页的标题。"

来源:GeeksForGeeks

from selenium import webdriver
import time
driver = webdriver.Firefox(executable_path=r"[PATH]")
driver.get("https://hapondo.qa/rent/doha/apartments/studio")
time.sleep(2)

print(driver.title)
#Output: Studio Apartments for rent in Doha | hapondo

变化很小的替代解决方案:

您需要处理以下几件事:

  • 要检索 页面标题 而不是使用 ,您需要使用 driver.title
  • hapondo website contains JavaScript 个启用的元素。

解决方案

要提取 页面标题,您需要引入 WebDriverWait for the title_contains() and you can use either of the following 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
    

参考资料

您可以在以下位置找到一些相关的详细讨论: