2017-06-28 37 views
0

xpathで正しい要素を取得できないため、エラーが発生します。 Webハンドルが変更されましたか? イメージをクリックできますか?私はエラーを解釈する方法を理解していません。セレンのリンクのない年齢確認をクリックする方法

from selenium import webdriver 
from selenium.webdriver.common.keys import Keys 
from selenium.webdriver.common.by import By 
from selenium.common.exceptions import NoSuchElementException 

driver = webdriver.Chrome() 

#browser.get('https://ebs.lcb.state.pa.us/OA_HTML/AppsLocalLogin.jsp') 
driver.get('http://www.finewineandgoodspirits.com') 
assert 'Fine Wine & Good Spirits: Shop Online for Wine and Spirits in Pennsylvania' in driver.title 

browser.find_element_by_xpath("xpath=//div[@id='ageVerify']/img[@alt='Yes']").click() 

InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath expression xpath=//div[@id='ageVerify']/img[@alt='Yes'] because of the following error: TypeError: Failed to execute 'evaluate' on 'Document': The result is not a node set, and therefore cannot be converted to the desired type. (Session info: chrome=59.0.3071.115) (Driver info: chromedriver=2.26.436362 (5476ec6bf7ccbada1734a0cdec7d570bb042aa30),platform=Windows NT 6.1.7601 SP1 x86_64)

+0

まずオフをクリックして要素あなたが存在をターゲットにしているところHTMLが切り取らにあなたの質問を更新することができました。次に、別の 'WebElement'ロケータを使用します。ブラウザのコンソールを開き、セレクタが有効かどうかを確認するためにjQueryを使い始めます( '$ x(// div [@ id = 'ageVerify']/img [@ alt = 'はい']' div#ageVerify img [alt = "はい"]) 'CSSパスの場合)。あなたのエラーはそれ以上冗長ではありません。 ( 'xpath exp ...で要素を見つけることができません。これは、セレクタがうまくいきません) – iamdanchiv

答えて

0

ので、あなたのXPathは何も返していない、あなたのXpathでdiv要素とIMGとの間で別のdivあります。

"xpath=//div[@id='ageVerify']/img[@alt='Yes']"から"//div[@id='ageVerify']/div/img[@alt='Yes']"に変更してください。クリックすることができます。

+0

これは、popupウィンドウを閉じるonclick()イベントを持つdivの間です。画像自体ではありません。画像をクリックすると何もできません。 –

0

webdriverを起動するにはdriver = webdriver.Chrome() ドライバを使用しており、正しくない画像をクリックするにはbrowser.find_element_by_xpath("xpath=//div[@id='ageVerify']/img[@alt='Yes']").click()ブラウザを使用しています。コードを続行するには単一の変数を使用する必要があります。

ポップアップ

  1. finewineandgoodspiritsへようこそ」
  2. はい

の3枚の画像は、サイト上で継続してはいをクリックしますので、あなたが更新する必要があります。クリックするxpathコード

driver.find_element_by_xpath("//div[@id='ageVerify']//img[@alt='Yes']").click() 

私はこのコードを使用していると私は、はいイメージ

from selenium import webdriver 

driver = webdriver.Chrome() 

driver.get('http://www.finewineandgoodspirits.com') 
assert 'Fine Wine & Good Spirits: Shop Online for Wine and Spirits in Pennsylvania' in driver.title 
driver.find_element_by_xpath("//div[@id='ageVerify']//img[@alt='Yes']").click() 
+0

ドライバ/ブラウザでご迷惑をおかけして申し訳ありません。私は投稿する前にそれを修正しなかった。私はそれが実際にそれを除いて正しいと信じることができません。助けてくれてありがとう! –

関連する問題