我正在用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)发布于 2018-11-08 16:16:37
我想你只是遇到了一些选择器的问题。我检查了元素并更新了数量设置;除了二进制位置之外,其他一切都应该基本相同。
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的情况下都运行过这个程序,它对我来说运行得很好。我在最后导航到主页,以便您可以确认数量更改有效(因此屏幕截图)。
发布于 2018-10-27 04:02:35
您看到的行为是什么?
当我启用headless时,脚本开始失败,因为运行headless会减慢执行速度。
我目前使用以下选项运行chrome:
'--no-sandbox', '--headless', '--window-size=1920,1080', '--proxy-server="direct://"', '--proxy-bypass-list=*'最后两个选项应该有助于解决速度慢的问题,但我看不出有什么区别。
希望这能有所帮助。
发布于 2018-11-02 05:15:25
我在我的苹果电脑上验证了你的声明(使用/Applications/Google Chrome.app/Contents/MacOS/Google Chrome)。
我的猜测是,由于您正在从一个商品页面移动到Amazon的购物车页面,cookies将丢失,因此购物车页面不会显示任何商品,因此不会包含任何名称以“quantity”开头的文本输入,这就是异常的原因。
在谷歌上搜索无头铬曲奇会得到this page,这反过来又指向this page,它的内容也可能是关于你的问题。无论是这样,还是亚马逊网站的一种特别聪明的行为,事实仍然是:存储购物车(或其密钥,但结果是相同的)的cookie在无头模式下不会被购物车页面读取。
https://stackoverflow.com/questions/52999029
复制相似问题