【问题标题】:Python selenium get page titlePython selenium 获取页面标题
【发布时间】:2021-02-23 20:21:11
【问题描述】:
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

【问题讨论】:

  • 您遇到的错误是什么?您使用了不正确的定位器 - css_selector 或 xpath ?
  • @Sureshmani 抱歉,现在刚刚更正。但仍未解决问题。
  • 您的 WebDriverWait 行在哪里?因为如果我在 print(driver.title) 之前拥有它,它对我来说没问题
  • @MatthewKing 我尝试了多次,但打印出空白。 repl.it/@AmericanY/issue#main.py

标签: python selenium selenium-webdriver webdriverwait page-title


【解决方案1】:

您需要注意以下几点:

  • 要检索页面标题而不是使用,您需要使用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
    

参考文献

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

【讨论】:

  • 如果页面标题像this,如何添加更多字符串?
  • @αԋɱҽԃαмєяιcαη 短语hapondo 是一个字符串,我确信它会出现在任何带有https://hapondo.qa 的网页的页面标题 中。您可以使用任何适合您要求的字符串。
  • @αԋɱҽԃαмєяιcαη 我查看了 Rent 部分,但无法确定 bedroomfurnishedapartmentin 或 @ 中的哪个字符串987654338@ 可以在所有出租物业的页面标题 中通用。您可以使用通用术语作为预期条件中的预期文本
【解决方案2】:

“页面标题”是指浏览器顶部选项卡上显示的文本。

对您的代码改动很小的解决方案:

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 并没有打印出来
  • @αԋɱҽԃαмєяιcαη 您应该更新您的代码以显示已设置无头选项,因为这与问题的原因有关
  • @αԋɱҽԃαмєяιcαη,不,为我工作:Studio Apartments for rent in Doha | hapondo 已输出
  • @MatthewKing 你是对的。无头选项是问题的原因。还在想办法。
  • @lionrocker221 抱歉,我注意到问题与无头浏览器有关。你有什么线索吗?
猜你喜欢
  • 2012-01-20
  • 2013-10-04
  • 1970-01-01
  • 2015-09-24
  • 1970-01-01
  • 2020-05-30
  • 1970-01-01
  • 1970-01-01
  • 2015-06-14
相关资源
最近更新 更多