2017-11-23 11 views
-1

私は初心者で趣味のプログラマーです。私はウェブサイトをループし、&の印刷データを取得しようとしています。コードは第1のものでは機能しますが、リンク元のリストの2番目の項目を「クリック」することはできません。次のように私のコードは次のとおりです。私は最初のリストから作業する2番目のクリックを取得するためのさまざまな方法を試してみましたし、次のどこへ行くかわからない2番目のループ/パスでxpathが見つかりません

from bs4 import BeautifulSoup as soup 
from selenium import webdriver 
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.common.by import By 

#set chromepath to include .exe 
chrome_path = r"C:\Program Files\Python37\chromedriver_win32\chromedriver.exe" 
driver = webdriver.Chrome(chrome_path) 

#set variables 
myurl='http://www.ctbar.org/search/newsearch.asp' 
loop_results = 1 
pagination = 1 
next_section = 1 
counter = 0 

driver.get(myurl) # loads the 1st page into the browser 

driver.switch_to_frame('SearchResultsFrame')#switches focus to the results frame 
id_results1 = driver.find_elements(By.XPATH,('//*[contains(@id,"MiniProfileLink")]'))# put results into an array 

while counter <= len(id_results1)-1: # for testing only print/see all the list 
    text_results1 = id_results1[(counter)].text 
    print (text_results1) 
    counter +=1 

id_results1[0].click() # click the first entry 
#grab the email address and print it 
email = driver.find_element_by_xpath ('//*[@id="SpContent_Container"]/table/tbody/tr[2]/td[3]/table[1]/tbody/tr/td/a').text 
print (email) 
driver.back()  
print ('second click') 
driver.switch_to_frame('SearchResultsFrame')#switches focus to the results frame 
id_results2 = driver.find_elements(By.XPATH,('//*[contains(@id,"MiniProfileLink")]')) # re-grab the results 
#for testing, put the results into a variable & print 
text_results2 = id_results2[2].text 
print (text_results2) 

print('Done') 
driver.quit() 

大変助かりました。

ありがとうございます。

+1

私はとにかくあなたの '二click'の試みは表示されません。私は '' driver.back() 'の直後に' print( 'second click') 'を見るだけです。何か不足していますか? – DebanjanB

答えて

0

私はクリックしようとしましたが、何も得られませんでした。私はリンクを最初に見たり/印刷したり、それをクリックして変換できるかどうかを見ることにしました。それを見ることができないので、私はそれをクリックすることもできないと思った。

0

私は2つの段階でこれを行うために、その簡単に言う:彼らはIDが

2から静的なURLのように見えるよう

1)人に、すなわち(すべてのプライマリリンクを集める)改ページを含む)の収集2番目のループの電子メール。

これにより、エラーが発生しやすくなり、読み込み時間がかかることが回避されます。

ハングロード時間を避けるために:

driver.set_page_load_timeout(30)# 30 second time limit. 
+0

私はそれをどうやってやろうとしていますか? – tonymgarner

関連する問題