我有一个简单的脚本导航到下面以黄色和绿色突出显示的以下页面。
我的问题是,如何创建一个页面计数器,以便我知道这个工作的位置?
理想情况下会显示:
Page 5
Page 4 #For each of the loop
以下内容可行,但我遇到以下问题:
elements = [x.get_text("*") for x in
wait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,'(//div[div/div/text()="Main Lists"]//div[starts-with(@class, "sm-CouponLink_Label") and normalize-space()]')))]
完整代码:
import collections
from random import shuffle
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait as wait
driver = webdriver.Chrome()
driver.set_window_size(1024, 600)
driver.maximize_window()
driver.get('https://www.bet365.com.au/#/AS/B1/')
driver.get('https://www.bet365.com.au/#/AS/B1/')
def page_counter():
for x in range(1000):
yield x
clickMe = wait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,'(//div[div/div/text()="Main Lists"]//div[starts-with(@class, "sm-CouponLink_Label") and normalize-space()])')))
options = driver.find_elements_by_xpath('//div[div/div/text()="Main Lists"]//div[starts-with(@class, "sm-CouponLink_Label") and normalize-space()]')
indexes = [index for index in range(len(options))]
shuffle(indexes)
for index in indexes:
count = page_counter()
driver.get('https://www.bet365.com.au/#/AS/B1/')
elements = [x.get_text("*") for x in
wait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,'(//div[div/div/text()="Main Lists"]//div[starts-with(@class, "sm-CouponLink_Label") and normalize-space()]')))]
#elements = [x.get_attribute("href") for x in
# driver.find_elements_by_xpath('//div[div/div/text()="Main Lists"]//div[starts-with(@class, "sm-CouponLink_Label") and normalize-space()]')))]
clickMe.click()
shuffle(elements)
links = dict((next(count) + 1, e) for e in elements)
desc_links = collections.OrderedDict(sorted(links.items(), reverse=True))
for key, value in desc_links.items():
try:
driver.get(value)
print('Page ' + str(key))
except TimeoutException as ex:
pass
错误:
line 36, in <module>
wait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,'(//div[div/div/text()="Main Lists"]//div[starts-with(@class, "sm-CouponLink_Label") and normalize-space()]')))]
File "C:\Users\Django\AppData\Local\Continuum\miniconda3\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
答案 0 :(得分:0)
主列表下的列表不是hrefs,你不能让驱动程序使用driver.get()打开所有列表(至少据我所知),你必须逐个点击所有列表并获取信息。
看看这段代码是否有助于入门,用更好的等待逻辑替换time.sleep
日期 開盤指數 最高指數 最低指數 收盤指數
0 96/02/01 7751.30 7757.63 7679.78 7701.54
1 96/02/02 7754.16 7801.63 7751.53 7777.03
2 96/02/05 7786.77 7823.94 7772.05 7783.12
3 96/02/06 7816.30 7875.75 7802.94 7875.75
4 96/02/07 7894.77 7894.77 7850.06 7850.06
<强>更新强>
driver.get('https://www.bet365.com.au/#/AS/B1/')
driver.find_element_by_id('TopPromotionBetNow').click()
time.sleep(10)
classifications = driver.find_elements_by_class_name('wn-Classification')
for classification in classifications:
if classification.text == 'Soccer':
classification.click()
break
time.sleep(10)
markets = driver.find_elements_by_class_name('sm-Market')
for market in markets:
group_name = market.find_element_by_class_name('sm-Market_GroupName')
if group_name.text == 'Main Lists':
coupon_lables = [x for x in market.find_elements_by_class_name('sm-
CouponLink_Label')]
break
for label in coupon_lables:
print('executing:' + label.text)
label.click()
# do your stuff
# find the back button (<) on the web page, click on it
# go back to select the next label