我正在制作一个机器人来自动购买枪底火,我需要它点击一个下拉按钮来显示子弹的数量

2024-04-29 18:51:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我爸爸的一个朋友想让我做一个机器人,它可以自动购买枪支底漆,因为它们总是卖完。我有一个带有一些弹药的脚本作为测试(因为它实际上在库存中),但当我试图找出如何单击下拉列表时,终端总是会抛出这个错误

selenium.common.exceptions.InvalidArgumentException: Message: invalid type: null, expected a string at line 1 column 12

作为参考,我将Python 3.9与Selenium 3.141.0结合使用,以下是我的代码:

#imports funcs from selenium
from selenium import webdriver

#chooses your browser

driver = webdriver.Firefox()

#gets your store
url = driver.get("https://www.midwayusa.com/product/2090655809")

#opens the website
driver.get(url)

#makes variables for html elements
button = driver.find_element_by_xpath('//*[@id="productSelectorContainer"]/div[1]/button')

#le button click has arrived
button.click()

Tags: fromurlyourgetdriverselenium机器人朋友
2条回答

缺点:

url = driver.get("https://www.midwayusa.com/product/2090655809")

不是有效的直接使用驱动程序。get('url')

您需要单击同意,否则它会与所有单击重叠

下面是一个示例,单击手头50个数量的项目,然后继续

wait = WebDriverWait(driver, 5)
driver.get("https://www.midwayusa.com/product/2090655809")
driver.maximize_window()
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"#cookie-consent-btn"))).click() 
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"#productSelectorContainer > div.product-filter.original-selector > button"))).click()
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"#Quantity > ol > li:nth-child(1) > button"))).click()
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"button.big-primary-button.add-to-cart-button"))).click()

进口

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC

输出

enter image description here

我想我看到了这个问题。你有这两行

#gets your store
url = driver.get("https://www.midwayusa.com/product/2090655809")

#opens the website
driver.get(url)

driver.get()不返回任何内容,因此null被存储到url。然后在下一行中,您将导航到null。这些行实际上是多余的,看起来您误解了如何使用它们。您只需要第一行,并进行一些更正

我删除了多余的一行,更正了另一行,并对您的一些评论进行了一些调整,使其更加准确

#imports funcs from selenium
from selenium import webdriver

#launches the browser
driver = webdriver.Firefox()

#navigates to your store website
driver.get("https://www.midwayusa.com/product/2090655809")

#stores the button in a variable
button = driver.find_element_by_xpath('//*[@id="productSelectorContainer"]/div[1]/button')

#le button click has arrived
button.click()

相关问题 更多 >