[英]Clicking button on webpage using Selenium Python Beautifulsoup
I'm trying to create a 'bot' to buy a graphics card.我正在尝试创建一个“机器人”来购买显卡。 I've downloaded a pre-made script, and i'm trying to adjust to my needs.我已经下载了一个预制的脚本,我正在尝试适应我的需要。
Script take me to the site in Firefox, finds the button I am trying to look for using the following code:脚本将我带到 Firefox 中的站点,使用以下代码找到我要查找的按钮:
findAllCards = soup.find('button', {'class': 'Button__StyledButton-iESSlv dJJJCD Button-dtUzzq kHUYTy'})
This works.这行得通。 However, when I an trying to click the button, I am unable to as I have no idea what I am suppose to find here: driverWait(driver, 'css', '.space-b center')
但是,当我尝试单击按钮时,我无法做到,因为我不知道我想在这里找到什么: driverWait(driver, 'css', '.space-b center')
Webpage I'm using to test is: https://www.currys.co.uk/gbuk/gaming/console-gaming/controllers/xbox-wireless-controller-carbon-black-10211565-pdt.html我用来测试的网页是: https://www.currys.co.uk/gbuk/gaming/console-gaming/controllers/xbox-wireless-controller-carbon-black-10211565-pdt.ZFC35FDC70D5FC69D269883A822C7A5E
Full code here:完整代码在这里:
driver.get(url)
while True:
html = driver.page_source
soup = bs4.BeautifulSoup(html, 'html.parser')
wait = WebDriverWait(driver, 15)
wait2 = WebDriverWait(driver, 2)
try:
findAllCards = soup.find('button', {'class': 'Button__StyledButton-iESSlv dJJJCD Button-dtUzzq kHUYTy'})
if findAllCards:
print(f'Button Found!: {findAllCards.get_text()}')
# Clicking Add to Cart.
time.sleep(.3)
print('Click')
driverWait(driver, 'css', '.space-b center')
print('Click1')
time.sleep(2)
Thanks:)谢谢:)
Your findAllCards
above returns 3 web elements, not 1. Assuming you are trying to click on the Add to Basket button:您上面的findAllCards
返回 3 个 web 元素,而不是 1。假设您尝试单击“添加到购物篮”按钮:
findAllCards = driver.find_element_by_xpath("//div[@id='product-actions']//div[@data-component='add-to-basket-button-wrapper']//button")
findAllCards.click()
Some odd thing with element to be interactable.元素可交互的一些奇怪的东西。
wait = WebDriverWait(driver, 10)
driver.get(url)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"button#onetrust-accept-btn-handler"))).click()
try:
elem=wait.until(EC.presence_of_element_located((By.XPATH,"//button[contains(.,'Add to basket')]")))
driver.execute_script("arguments[0].click();", elem)
except Exception as e:
print(str(e))
Import进口
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.