无头Webdriver返回错误,但无头工作

时间:2018-10-25 22:36:11

标签: 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
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)

3 个答案:

答案 0 :(得分:0)

您看到的是什么行为?

启用无头时,脚本开始失败,因为无头运行会降低执行速度。

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

'--no-sandbox', '--headless', '--window-size=1920,1080', '--proxy-server="direct://"', '--proxy-bypass-list=*'

最后两个选项应该有助于缓解速度,但我看不出有什么区别。

希望这会有所帮助。

答案 1 :(得分:0)

我已在Mac上使用/Applications/Google Chrome.app/Contents/MacOS/Google Chrome验证了您的索赔。

我的猜测是,由于您是从Amazon的商品页面移至购物车页面,因此cookie丢失了,因此购物车页面将不会显示任何商品,因此不会包含任何带有以“ quantity”开头的名称,这就是例外。

搜索无头铬cookie 会得到this page,它依次指向this page可能的内容也与您的问题有关。无论是这种情况,还是亚马逊网站特别聪明的行为,事实仍然存在:在无头模式下,购物车页面不会读取存储购物车的Cookie(或其键,但结果是相同的)

答案 2 :(得分:0)

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

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和不运行<form id='form' action='https://m.youtube.com/watch?v=' method='get'> <input type='hidden' name='v' id='v' value='XgQrzkvvOO4'> </form> 的情况下运行了,对我来说很好。我导航到最后的主页,因此您可以确认更改数量的工作(因此,截图)。

相关问题