2017-02-19 63 views
0

ウェブサイトにログインして、そのコンテンツをSeleniumでスクラップしようとしています。このサイトにはユーザーがパスワードを入力する仮想キーボードがあり、このキーボードのクリックをシミュレートする必要があります。サイトを点検Python Selenium - タイトルで検索して要素をクリック

https://www.rico.com.vc/

、それは、(各キーの位置はランダムにはJavaScriptによって生成されますが、その部分はOKです)キーボードが生成される部分です。

<div class="password-buttons"> 
<button class="button orange rounded" onclick="AddPsitions('8|9'); return false;" title="5 ou 3"> 
    <span class="login-number">5</span> 
    <span class="login-or">ou</span> 
    <span class="login-number">3</span> 
</button> 
<button class="button orange rounded" onclick="AddPsitions('6|7'); return false;" title="8 ou 2"> 
    <span class="login-number">8</span> 
    <span class="login-or">ou</span> 
    <span class="login-number">2</span> 
</button> 
<button class="button orange rounded" onclick="AddPsitions('4|5'); return false;" title="0 ou 6"> 
    <span class="login-number">0</span> 
    <span class="login-or">ou</span> 
    <span class="login-number">6</span> 
</button> 

シミュレートするために、私は次のことをやっているのクリックが(私も試してみました "// * [タイトル= '0 OU 6' @]" のない、 ''):

browser.find_element_by_xpath(".//*[@title='0 ou 6']").click() 

しかし、私はこのエラーを取得しています:

Traceback (most recent call last): 
File "webScrap_Rico.py", line 59, in <module> 
browser.find_element_by_xpath(a).click() 
File "/home/luciano/.local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 293, in find_element_by_xpath 
return self.find_element(by=By.XPATH, value=xpath) 
File "/home/luciano/.local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 752, in find_element 
'value': value})['value'] 
File "/home/luciano/.local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 236, in execute 
self.error_handler.check_response(response) 
File "/home/luciano/.local/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 192, in check_response 
raise exception_class(message, screen, stacktrace) 
selenium.common.exceptions.InvalidSelectorException: Message: {" errorMessage":"Unable to locate an element with the xpath expression \".//*[@title='0 ou 6']\" because of the following error:\nError: TYPE_ERR: DOM XPath Exception 52","request":{"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"109","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:60775","User-Agent":"Python-urllib/2.7"},"httpVersion":"1.1","method":"POST","post":"{\"using\": \"xpath\", \"sessionId\": \"8ddb4f90-f6e6-11e6-9eb0-4ba40f4453e7\", \"value\": \"\\\".//*[@title='0 ou 6']\\\"\"}","url":"/element","urlParsed":{"anchor":"","query":"","file":"element","directory":"/","path":"/element","relative":"/element","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/element","queryKey":{},"chunks":["element"]},"urlOriginal":"/session/8ddb4f90-f6e6-11e6-9eb0-4ba40f4453e7/element"}} 

私はここに、このオプションを見ました:Find and click element by title Python Selenium

は、私はここで何をしないのですか?

答えて

0

XPath //button[@title='7 ou 8']は私のために働いています。 $x()を使用してChromeブラウザでXPathをテストできます(例: $x("//button[@title='7 ou 8']")。それはあなたが正しいロケータをより速く見つけるのを助けるでしょう。

0

これらのボタンのタイトルは、サイトを開くたびに変更されるため、ボタンを見つけるために使用することはできません。私はあなたが

あなたが解決しようexpected_conditions

from selenium.webdriver.support import expected_conditions as EC 

wait = WebDriverWait(driver, 10) 
buttons = wait.until(EC.visibility_of_any_elements_located((By.XPATH, "//button[contains(@onclick, 'AddPsitions')]"))) 
buttons[0].click() 
buttons[1].click() 
#... 
+0

私は毎回変わるボタンについてこの質問を扱っています。とにかく、私はあなたのやり方を試して、このエラーが発生しました:[...] selenium.common.exceptions.InvalidElementStateException:メッセージ:{"errorMessage": "SyntaxError:DOM Exception 12" [...] –

+0

しかし、 : 'buttons = browser.find_elements_by_xpath(" // button [@ class = 'button orange rounded'] ")'ボタンを見つけることができましたが、このエラーが出ました: 'selenium.common.exceptions。ElementNotVisibleException:メッセージ:{"errorMessage": "要素が現在表示されておらず、操作できない可能性があります" –

+0

@lucianoaraujo私の答えを編集しました。ところで、xpath '" // button [@ class = 'button orange rounded'] "'はボタンのどれかを見つけません。 – Guy

0

で明示的な待機を使用することができ、それらをクリックする前に、要素が表示されていることを確認するためにそれらをクリックして、リストや使用インデックスに部分的onclick属性を使用して、すべてのボタンの位置を示唆します!

まだタイトルで要素をクリックできませんでしたが、 'title'ではなく 'class'でクリックする要素を見つける回避策が見つかりました。

browser.find_elements_by_xpath("//button[contains(@class, 'button orange rounded')]") 

そしてインデックスを持つ要素をクリック:

a[click].click() 

私はまた、次のような問題持っていた:私は最初のキーボードを選択していなかったため

selenium.common.exceptions.ElementNotVisibleException: Message: {"errorMessage":"Element is not currently visible and may not be manipulated 

そして、それが起こっていたが。 セレンは、文字通りユーザーアクションをシミュレートしているので、私が最初に仮想キーボードを選択する必要があり、そうセレンは、キー「見る」ことができます。すべての助けを

browser.find_element_by_id("txtPassword").click() 

感謝を!

関連する問題