我有以下代码:
browser.find_element_by_css_selector('#bet').send_keys(2500)
time.sleep(2)
x = 0
while x < 100:
browser.find_element_by_css_selector('#start_game').click()
time.sleep(3)
browser.find_element_by_css_selector('li.tile:nth-child('+str(random.randint(1, 25))+')').click()
time.sleep(2)
browser.find_element_by_css_selector('li.tile:nth-child('+str(random.randint(1, 25))+')').click()
time.sleep(2)
browser.find_element_by_css_selector('li.tile:nth-child('+str(random.randint(1, 25))+')').click()
time.sleep(5)
try:
browser.find_element_by_css_selector('.cashout').click()
except NoSuchElementException:
browser.find_element_by_css_selector('#start_game').click()
time.sleep(10)
x = x+1
好吧,我的代码用比特币玩一个minesweaper游戏。所有作品都很棒,他下注,开始游戏随机点击3框。当他成功点击3个盒子并且没有炸弹时出现了现金输出按钮,但当脚本点击炸弹时,没有出现提款按钮,我得到了nosuchelement。
我试过用:
try:
browser.find_element_by_css_selector('.cashout').click()
except NoSuchElementException:
browser.find_element_by_css_selector('#start_game').click()
如果他点击炸弹和提款似乎没有开始新游戏......但它没有用。有人能帮助我吗?
答案 0 :(得分:2)
你真的需要切换到Explicit Waits而不是硬编码的睡眠呼叫。
等待cashout
元素变为可见,然后点击:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
try:
element = WebDriverWait(browser, 10).until(
EC.visibility_of_element_located((By.CSS_SELECTOR, ".cashout"))
)
element.click()
except TimeoutException:
browser.find_element_by_css_selector('#start_game').click()