2017-12-20 1 views
0

以下の黄色と緑色で強調表示された次のページに移動する簡単なスクリプトがあります。CSSの要素のページカウンタ

私の質問は、ページカウンタを作成するためにどのようにして仕事がどこにあるのか分かりますか?

理想的には、それは読んでいました:

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: 

enter image description here

+2

'待機(ドライバ、10)とtime.sleepを置き換える、始めるのに役立ちます場合

を参照してください。 element_to_be_clickable(locator)) 'あなたに返すつもりでないWebElementは、反復可能ではありません。 'wait(driver、10).until(EC.visibility_of_all_elements_located(locator))'を試してください。また、get_text()関数の定義を追加してください。 – Andersson

+0

@Anderssonこの場合、get_textはどのように見えますか、CSSを追加すると同じエラーが発生します –

+1

'get_text()'について私に尋ねますか?私はどのように見えるかわからない - あなたのコード、あなたの関数:) * ...私がCSSを追加すると... *何に追加?問題を詳細に記述し、正確なエラーログを提供してください – Andersson

答えて

0

メインリストの下のリストはのhrefされていないと、あなたはドライバーが(少なくとも私の知る限り)driver.getを()を使用して、すべてのリストを開くことはできません、あなたがクリックする必要があるだろうすべてのリスティングを1つずつ情報を入手してください。このコードは、より良い待機ロジック(EC .until

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 

更新

coupon_lables = [x.text for x in market.find_elements_by_class_name('sm-CouponLink_Label')] 

for label in coupon_lables: 
    time.sleep(5) 
    driver.find_element_by_xpath(f'//div[contains(text(), "' + label + '")]').click() 
    time.sleep(5) 
    driver.find_element_by_class_name('cl-BreadcrumbTrail_BackButton').click() 
+0

メッセージ:古い要素参照:要素がページ文書に添付されていません - https://pastebin.com/LHna0MCr –

+0

どの行がそのエラーをスローしていますか?私は自分のsystsmでの作業として複製できません – Satish

+0

wait(driver、5).until(EC.element_to_be_clickable((By.CSS_SELECTOR、( "wn-Classification")))))または同等の。 time.sleepは機能していないようです。 –