Python中的Selenium 4与Edge Option无头错误合作,但不适合

发布于 2025-01-24 22:48:07 字数 2535 浏览 3 评论 0原文

因此,我的功能可以从网站上获取一些信息( https://www.fragrantica.com/perfume/dior/sauvage-eau-de-parfum-48100.html ,我想获得评分)。我已经安装了Selenium 4和WebDriver_Manager来照顾我的驾驶员等。

当我使用无头选项时,我会发现“无法找到元素”错误,但是当我评论时,它可以正常工作。我尝试使用Edge无头在另一个网站上使用Edge无头(但这是一周前),但它似乎有效。 这是代码:

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

你们知道如何解决这个问题吗?我一直在试图找到一个解释,但没有成功。始终弹出浏览器将是不便的。

非常感谢!

So I have function which takes some information from a website (https://www.fragrantica.com/perfume/Dior/Sauvage-Eau-de-Parfum-48100.html ; and I want to take ratings). I have selenium 4 installed and webdriver_manager to take care of my drivers, among others.

When I use the headless option I get the 'Unable to locate element' error, but when I comment it out it works just fine. I tried using Edge headless for another site (but that was a week ago) and it seemed to work.
Here is the code:

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

Do you guys have any idea how to work around this? I have been trying to find an explanation, but with no success. It would be inconvenient to have the browser pop up all the time.

Thank you very much!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挽手叙旧 2025-01-31 22:48:07

导入EdgeOptions来自selenium.webdriver and Pass head> head> head> head使用add_argument()&将硒更新为最新

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

Import EdgeOptions from selenium.webdriver and pass headless arg using add_argument() & Update Selenium to Latest

from selenium.webdriver import EdgeOptions
options = EdgeOptions()
options.add_argument("--headless")
数理化全能战士 2025-01-31 22:48:07

我以前遇到过这种问题。在这种情况下,问题的原因是使用较旧版本的浏览器以无头模式为边缘。渲染页面在旧版本中有所不同,因此无法找到元素。

我认为您问题的原因也可能就是这个。您可以尝试通过添加用户代理参数user-agent = user-agent:mozilla/5.0(Windows NT 10.0; win64; x64)AppleWebkit/537.36(KHTML,像Gecko)Chrome/chrome/chrome/chrome/charome/ 96.0.4664.110 Safari/537.36 EDG/101.0.1210.32解决问题。您可以将用户代理中的边缘版本更改为自己。

参考链接:在Outlook上运行硒无头测试显示Edge Chromium浏览器上的Outlook的旧版本

I ran into this kind of issue before. In that case, the cause of the issue is Edge using an older version of browser in headless mode. The rendering page is different in old version, so the element can't be located.

I think the cause of your issue also might be this. You can try to override the user-agent by adding the user-agent argument 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 to fix the issue. You can change the Edge version in the user-agent to your owns.

Ref link: Running Selenium Headless Tests on Outlook shows older version of outlook on Edge chromium browser

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文