Python Headless webdriver返回错误,但非Headless有效

Python Headless webdriver返回错误,但非Headless有效,python,selenium-webdriver,Python,Selenium Webdriver,我正在用Amazon和Webdriver做一个简单的实验。然而,使用Webdriver Headless无法找到元素和错误,但非Headless可以工作 有什么建议可以让它无头工作吗? 在--headless标志的正上方有一条注释 from selenium import webdriver import sys import os from selenium import webdriver from selenium.webdriver.common.keys import Keys fr

我正在用Amazon和Webdriver做一个简单的实验。然而,使用Webdriver Headless无法找到元素和错误,但非Headless可以工作

有什么建议可以让它无头工作吗? 在--headless标志的正上方有一条注释

from selenium import webdriver
import sys
import os

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


def get_inventory(url):

    chrome_options = Options()

    # Making it headless breaks. Commenting
    # this line, making it non-headless works.  
    chrome_options.add_argument("--headless")

    chrome_options.add_experimental_option(
        "prefs", {'profile.managed_default_content_settings.javascript': 2})
    chrome_options.binary_location = '/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary'

    driver = webdriver.Chrome(executable_path=os.path.abspath(
        'chromedriver'), chrome_options=chrome_options)

    driver.set_window_size(1200, 1000)

    try:
        driver.get(url)

        add_to_cart_button_xp = '//*[@id="add-to-cart-button"]'
        add_to_cart_button = driver.find_element_by_xpath(add_to_cart_button_xp)
        add_to_cart_button.click()

        driver.get('https://www.amazon.com/gp/cart/view.html/ref=lh_cart')

        qty_field_xp = '//div/input[starts-with(@name, "quantity.") and @type="text"]'
        qty_field = driver.find_element_by_xpath(qty_field_xp)
        qty_field.clear()
        qty_field.send_keys("2")

        update_link_xp = f'//input[@value="Update" and @type="submit"]'
        update_link = driver.find_element_by_xpath(update_link_xp)
        update_link.click()



url = 'https://www.amazon.com/Pexio-Professional-Stainless-Food-Safe-Dishwasher/dp/B07BGBSY9F'
get_inventory(url)

你看到的行为是什么

当我启用headless时,脚本开始失败,因为运行headless会降低执行速度

我目前使用以下选项运行chrome:

'--no-sandbox', '--headless', '--window-size=1920,1080', '--proxy-server="direct://"', '--proxy-bypass-list=*'
最后两个选项应该有助于降低速度,但我没有看到任何区别


希望这有帮助。

我在我的Mac电脑上验证了你的声明(使用
/Applications/Google Chrome.app/Contents/MacOS/Google Chrome

我的猜测是,由于您正在从一个项目页面移动到Amazon的购物车页面,Cookie会丢失,因此购物车页面不会显示任何项目,因此不会包含任何以“数量”开头的文本输入,这就是例外情况


谷歌搜索无头chrome Cookie的结果是,这反过来又指向,其内容也可能与你的问题有关。不管是这样,还是亚马逊网站的一个特别聪明的行为,事实仍然是:在无头模式下,存储购物车的cookie(或其密钥,但结果是相同的)不会被购物车页面读取。

我想你只是遇到了一些选择器问题。我检查了元素并更新了数量设置;除二进制位置外,其他所有内容都应该大致相同

from selenium import webdriver
import sys
import os

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


def get_inventory(url):
    chrome_options = Options()

    chrome_options.add_argument("--headless")

    driver = webdriver.Chrome(
        executable_path='/usr/bin/chromedriver', 
        chrome_options=chrome_options,
    )
    chrome_options.add_experimental_option(
        "prefs", 
        {'profile.managed_default_content_settings.javascript': 2},
    )
    chrome_options.binary_location = '/usr/bin'

    driver.set_window_size(1200, 1000)

    try:
        driver.get(url)
        driver.save_screenshot("/tmp/x1.png")

        driver.find_element_by_xpath('//*[@id="add-to-cart-button"]').click()

        driver.get('https://www.amazon.com/gp/cart/view.html/ref=lh_cart')

        driver.find_element_by_xpath("//span[@data-action='a-dropdown-button']").click()
        driver.find_element_by_xpath("//*[@class='a-dropdown-link'][text()[contains(., '2')]]").click()

        driver.find_element_by_class_name("nav-logo-base").click()

        driver.save_screenshot("/tmp/confirm.png")
        driver.close()
    except Exception as e:
        print(e)


url = 'https://www.amazon.com/Pexio-Professional-Stainless-Food-Safe-Dishwasher/dp/B07BGBSY9F'
get_inventory(url)

我已经运行了这个有无
--headless
,它对我来说运行得很好。最后我导航到了主页,这样你就可以确认更改的数量(因此屏幕截图)。

没有找到元素,它会出错。非接触性异常。。。。因此,完全相同的脚本在非headless模式下运行良好,但不在headless模式下运行。测试是否与cookies有关。您可能希望切换到FireFox驱动程序。我也遇到了无头Chrome的问题。一旦我切换到FireFox,它就消失了。