首页 > 解决方案 > 无头网络驱动程序返回错误但非无头工作

问题描述

我正在用 Amazon 和 Webdriver 做一个简单的实验。但是,使用 Webdriver Headless 无法找到元素和错误,但 non-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)

标签: pythonselenium-webdriver

解决方案


我认为您只是遇到了一些选择器问题。我检查了元素并更新了数量设置;除了二进制位置之外,其他一切都应该几乎相同。

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它,它对我来说工作正常。我导航到最后的主页,以便您确认数量更改有效(因此是屏幕截图)。


推荐阅读