python - headless webdriver 返回错误但非 headless 工作

标签 python selenium-webdriver

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

最佳答案

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

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 的情况下运行了它,它对我来说工作正常。最后我导航到主页,这样您就可以确认数量更改是否有效(因此是屏幕截图)。

关于python - headless webdriver 返回错误但非 headless 工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52999029/

相关文章:

python - Flask无法获取远程客户端的真实IP

python - Pandas 直方图标签和标题

java - Selenium webdriver 项目(Java)的架构(如文件结构)易于维护且不使用任何构建工具

javascript - 如何使 Selenium 忽略/跳过或覆盖 window.close()

html - 我需要 CSS 选择器来选择包含给定文本的元素

Python:写入具有多个标题行的 CSV

python - ansible 2.3.1.0 仅从文件安装

Selenium WebDriver - Xpath(或任何东西)来定位表中单元格旁边的单元格?

javascript - 如何在 Selenium WebDriver 中使用 JavascriptExecuter 设置属性值

python - 如何在 pandas 数据框中用 Series 压平列?