2016-10-29 3 views
0

私はthis websiteを掻き集めるつもりです。私はこの数週間、これをきちんとやってきましたが、今日は、宣伝を示し、そのページの要素がクリックされるのをブロックする宣伝バナーを追加しました。 (それはない新しいウェブサイトに私を再指示し、このバナーを閉じ、Xがあり、それだけでバナーを非表示にして、ページ内のすべての要素クリッカブルもう一度を作る。ページをブロックするWebバナーを閉じるSelenium

を私がしようとしています

browser.find_element_by_xpath("//*[@id={0}]".format(bannerid)).click() 

banneridが"sas_closeButton_6077476"ですが、私はエラー

ファイル "/home/vladimir/Documents/Projects/Real-Estate_Market/metrocuadrado_get_links.py"、 ライン52、get_links close_banner(ドライバー、中を入手"sas_closeB utton_6077476 ")ファイル" /home/vladimir/Documents/Projects/Real-Estate_Market/metrocuadrado_get_links.py "、 12行目、close_banner browser.find_element_by_xpath(" // [@ id = {0}]。 bannerid))。()をクリックしfind_element_by_xpath 戻りself.find_elementで ファイル "/home/vladimir/anaconda3/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py"、 ライン293、 (By = By.XPATH、値= xpath)ファイル "/home/vladimir/anaconda3/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py"、 行752、find_element '値':value})[' value ']ファイル "/home/vladimir/anaconda3/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py"、 行236、実行 self.error_handler.check_response(応答)ファイル "/home/vladimir/anaconda3/lib/python3.5/site-packages/selenium/webdriver/remote/errorhandler.py"、 行192、check_response内 raise exception_class(メッセージ、screen、stacktrace)selenium.common.exceptions.NoSuchElementException:メッセージ:いいえ 要素:要素を見つけることができません: {"method": "xpath"、 "selector": "// [@ id = sas_closeButton_6077476]"私は外にするかわからない

}
(セッション情報:クロム= 53.0.2785.143)( chromedriver = 2.22、プラットフォーム=のLinux 4.4.0-45-ジェネリックx86_64版ドライバ情報)この。基本的に要素は存在しません(ただし、HTMLコードを見ると、この要素はバナーの「生活時間」に対応しています)。

あなたの助け

UPDATE:。

私は今、このコードで働いています:各ページからいくつかの情報を得るためにページネータを反復処理より多くのコードがそこに行く

def close_banner(browser, bannerid): 
    browser.find_element_by_xpath("//*[@id='{0}']".format(bannerid)).click() 


driver = webdriver.Chrome("/usr/lib/chromium-browser/chromedriver") 

website = "http://www.metrocuadrado.com/web/inmuebles/venta/" 
driver.get(website) # loads the page 
time.sleep(7) 

# filters sites from Bogota 
ciudad_filtro = driver.find_element_by_class_name("ciudad_filtro") 
while True: 
    try: 
     ciudad_filtro.click() 
     break 
    except selenium.common.exceptions.WebDriverException: 
     close_banner(driver, "sas_closeButton_6077476") 

time.sleep(1) 
ciudades = ciudad_filtro.find_elements_by_css_selector("option") 
for ciudad in ciudades: 
    if ciudad.text == "Bogotá D.C.": 
     ciudad.click() 
     break 
time.sleep(10) 

# orders sites from the newest 
ordenar_filtro = driver.find_element_by_id("rb_ordenar") 
while True: 
    try: 
     ordenar_filtro.click() 
     break 
    except selenium.common.exceptions.WebDriverException: 
     close_banner(driver, "sas_closeButton_6077476") 
time.sleep(1) 
ordenes = ordenar_filtro.find_elements_by_css_selector("option") 
for orden in ordenes: 
    if orden.text == "Más nueva": 
     orden.click() 
     break 

このコードの後。このコードではclose_banner関数も使用されていますが、コピーするには時間がかかりすぎます。しかし、間違いは上記のコードで発生します。また、セレン・モジュールの待ち時間ではなく、time.sleep()のWebサイトを待っていることをお詫びして申し訳ありません。私のiPad

enter image description here

+0

これは有効なセレクタではない* // [@ id = sas_closeButton_6077476]のスタックトレースを参照してください。*またはタグがありません。正しいセレクタが渡されていることを確認してください。また、 Trueの場合と同様に、要素の数秒待つ必要があります。要素が見つかった場合は、それをクリックします。また、ページが最初に読み込まれていることを確認します。 – lauda

+0

バナーが常に表示されたら、ボタンが表示されるまで待ち、クリックします。 – lauda

答えて

1

から

これは、バナーの図であり、少なくとも一つの問題である - プレースホルダの周りに欠落している引用符は、交換してください:

"//*[@id={0}]".format(bannerid) 

と:

"//*[@id='{0}']".format(bannerid) 

または、find_element_by_id()直接法:あなたはまた、「タイミング」の問題を持つ可能性があります

browser.find_element_by_id(bannerid).click() 

注意。 wait for the presence of the element via WebDriverWait and presence_of_element_located expected conditionを試してください。

+0

私はidで要素を試しましたが、機能しませんでしたので、要素がクリック可能であるのを待って確認しましょう。 –

+0

これで、 'selenium.common.exceptions.ElementNotVisibleException:Message:element not visible'が表示されます。 –

+0

@VladimirVargas大丈夫です、今実行しているコードで質問を更新できますか?ありがとう。 – alecxe

関連する問題