私は初心者で趣味のプログラマーです。私はウェブサイトをループし、&の印刷データを取得しようとしています。コードは第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()
。
大変助かりました。
ありがとうございます。
私はとにかくあなたの '二click'の試みは表示されません。私は '' driver.back() 'の直後に' print( 'second click') 'を見るだけです。何か不足していますか? – DebanjanB