【问题标题】:Selenium 4 in Python works with Edge option headless False, but not with TruePython 中的 Selenium 4 适用于 Edge 选项 headless False,但不适用于 True
【发布时间】:2022-08-19 07:53:42
【问题描述】:

所以我有从网站获取一些信息的功能(https://www.fragrantica.com/perfume/Dior/Sauvage-Eau-de-Parfum-48100.html;我想获得评级)。我安装了 selenium 4 和 webdriver_manager 来管理我的驱动程序等。

当我使用无头选项时,我得到“无法定位元素”错误,但是当我将其注释掉时,它工作得很好。我尝试将 Edge headless 用于另一个站点(但那是一周前的事),它似乎有效。 这是代码:

import os
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.edge.options import Options
from webdriver_manager.microsoft import EdgeChromiumDriverManager


def get_info(url):
    \'\'\'Get all the ratings from fragrantica site.\'\'\'
    os.environ[\'WDM_LOCAL\'] = \'1\'
    os.environ[\'WDM_LOG_LEVEL\'] = \'0\'
    options = Options()
    options.headless = True
    options.add_experimental_option(\'excludeSwitches\', [\'enable-logging\'])

    driver = webdriver.Edge(service=Service(
        EdgeChromiumDriverManager().install()), options=options)

    try:
        driver.get(url)
        lst = []
        name = driver.find_element(
            By.XPATH, \"//h1[contains(@class,\'text-center medium-text-left\')]\").text
        WebDriverWait(driver, 30).until(ec.presence_of_element_located((By.XPATH, \'//*[@id=\"main-content\'
                                                                                  \'\"]/div[1]/div[\'
                                                                                  \'1]/div/div[2]/div[\'
                                                                                  \'4]/div[2]/div/div[\'
                                                                                  \'1]/div[3]/div/div\')))
        ratings = driver.find_elements(By.XPATH,
                                       \'.//div[@style=\"width: 100%; height: 0.3rem; border-radius: 0.2rem; \'
                                       \'background: rgba(204, 224, 239, 0.4);\"]\')
        votes = driver.find_element(
            By.XPATH, \"//span[contains(@itemprop,\'ratingCount\')]\").text
        for style in ratings:
            lst.append(style.find_element(
                By.TAG_NAME, \'div\').get_attribute(\'style\'))
        driver.quit()
        return name, lst, votes
    except:
        driver.quit()
        raise

你们知道如何解决这个问题吗?我一直在努力寻找解释,但没有成功。一直弹出浏览器会很不方便。

非常感谢!

    标签: python selenium selenium-webdriver webdriver microsoft-edge


    【解决方案1】:

    我以前遇到过这种问题。在这种情况下,问题的原因是 Edge 在无头模式下使用旧版本的浏览器。旧版本的渲染页面不同,无法定位元素。

    我认为您的问题的原因也可能是这个。您可以尝试通过添加用户代理参数user-agent=User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36 Edg/101.0.1210.32 来覆盖用户代理以解决此问题。您可以将用户代理中的 Edge 版本更改为您自己的。

    参考链接:Running Selenium Headless Tests on Outlook shows older version of outlook on Edge chromium browser

    【讨论】:

    • 嗨@Alexandru-GabrielBadea 我可以知道您的问题是否已解决?如果没有,请在这里分享。我们可以共同努力解决这个问题。
    • 是的,我试过了,还是不行。我更新了所有内容,但它仍然无法在无头模式下工作。也许更改浏览器会起作用?听起来更容易
    • 换其他浏览器后还能用吗?
    【解决方案2】:

    selenium.webdriver 导入EdgeOptions 并使用add_argument() 传递headless arg 并将Selenium 更新为最新

    from selenium.webdriver import EdgeOptions
    options = EdgeOptions()
    options.add_argument("--headless")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-14
      • 1970-01-01
      • 1970-01-01
      • 2011-11-16
      • 2019-11-15
      • 1970-01-01
      • 2019-02-15
      • 1970-01-01
      相关资源
      最近更新 更多