2017-03-23 1 views
0

問題文:以下のコードに達するまで、ウェブサイト上のさまざまなリンクをナビゲートしてデータをスクラップしようとしています「患者リスト」を指し示すハイパーテキスト参照に対応する。PythonでSeleniumをWebスクラップする:hrefタグでラップされた関数を呼び出すjavascriptを持つリンクをクリックできません

<td height="20" align="middle" nowrap=""> 
<a href="javascript:goPatientList()"><b>Patient List</b></a> 
</td> 

以下に示すように、ハイパーテキストリファレンスのxpathをコードにコピーしました。


以下のセクションにはPythonコードがあります。

import selenium 

from selenium import webdriver 

from selenium.webdriver.common.keys import Keys 

from selenium.webdriver.common.by import By 

from selenium.webdriver.support.ui import WebDriverWait 

from selenium.webdriver.support import expected_conditions as EC 

driver=webdriver.Chrome() 

driver.get("https://example.com") 

print(driver.title) 



username = driver.find_element_by_name('username') 

username.send_keys("username") 

password = driver.find_element_by_name("password") 

password.send_keys("password") 

driver.find_element_by_name("Login").click() 


driver.find_element_by_id("tab5").click() 

driver.find_element_by_id("menu5_3").click() 

**driver.find_element_by_xpath("/html/body/form/table/tbody/tr/td/table/tbody/tr[1]/td[7]/a").click()** 

私が受け取ったエラー:

Traceback (most recent call last): 
    File "login.py", line 31, in <module> 
    driver.find_element_by_xpath("/html/body/form/table/tbody/tr/td/table/tbody/tr[1]/td[7]/a").click() 

    File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 295, in find_element_by_xpath 
    return self.find_element(by=By.XPATH, value=xpath) 

    File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 756, in find_element 
    'value': value})['value'] 

    File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 238, in execute 
    self.error_handler.check_response(response) 

    File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 193, in check_responseraise exception_class(message, screen, stacktrace) 
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/form/table/tbody/tr/td/table/tbody/tr[1]/td[7]/a"} 
    (Session info: chrome=56.0.2924.87) 
    (Driver info: chromedriver=2.28.455517 (2c6d2707d8ea850c862f04ac066724273981e88f),platform=Mac OS X 10.11.6 x86_64) 

誰かがコードの一部を確認し、この過程で私を導くことはできますか?

答えて

0

NoSuchElementExceptionは、セレンが元素を見つけられなかったことを意味します。おそらくあなたが使用しているxpathは間違っています。あなたのxpathを確認してください。 Firefoxのfirebug拡張機能など、xpathをテストすることもできます。
人間と比較すると、セレンは速いです。セレンがページを読み込んでボタンをクリックしたときにボタンがない、またはJavaScriptが関連付けられていない可能性があります.Seleniumに少し待ってからクリックしてください。したがって、あなたのクリックの前に待機機能を追加してください。

driver.find_element_by_name("Login").click() 
driver.implicitly_wait(10) 
+0

地獄ハルン時間の最良の最もありません..お返事をいただき、ありがとうございます。私はfirefoxのfirebugを試して、私は同じxpathを取得します。 – Tintin

0

適切なxpathを作成する方法を学ぶ必要があります。 Firebugsは

https://www.w3schools.com/xml/xpath_syntax.asp

+0

フェリックスに感謝します。私はその文書を参照し、xpathの使い方を理解しようとします。 – Tintin

関連する問題