2 回答

TA貢獻(xiàn)1858條經(jīng)驗(yàn) 獲得超8個贊
我使用普通的 for 循環(huán)來迭代表而不是 for each 循環(huán)。試試這個,讓我知道它是怎么回事。
import csv
import time
from selenium import webdriver
browser = webdriver.Chrome('/usr/local/bin/chromedriver') # Optional argument, if not specified will search path.
browser.implicitly_wait(5)
browser.execute_script("window.open('about:blank','tab1');")
browser.switch_to.window("tab1")
browser.get(('https://e-sourcingni.bravosolution.co.uk/web/login.shtml'))
signInButton = browser.find_element_by_css_selector(".only")
signInButton.click()
time.sleep(5)
table = browser.find_element_by_css_selector(".list-table")
links=browser.find_elements_by_css_selector(".detailLink")
for i in range(len(links)):
links=browser.find_elements_by_css_selector(".detailLink")
links[i].click()
time.sleep(2)
browser.execute_script("window.history.go(-1)")
time.sleep(2)
with open('output.csv', "w") as f:
writer = csv.writer(f)
writer.writerow(["S.No","Status","Organization","Project Title","First Publishing Date","Work Category","Listing Deadline"])
table=browser.find_elements_by_xpath("//table[@class='list-table']//tr")
for row in range(len(table)):
x=[]
for d in browser.find_elements_by_xpath("//table[@class='list-table']//tr["+str(row)+"]//td"):
x.append(d.text.encode('utf-8'))
writer.writerow(x)
browser.close()

TA貢獻(xiàn)1809條經(jīng)驗(yàn) 獲得超8個贊
是的,因?yàn)槟苿拥较乱豁?,因?yàn)槟牧隧撁?,它無法在上一頁上找到該元素。你可以試試這個
import csv
import requests
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
browser = webdriver.Chrome(executable_path=r"D:\jewel\chromedriver.exe")
browser.execute_script("window.open('about:blank','tab1');")
browser.switch_to.window("tab1")
browser.get("https://e-sourcingni.bravosolution.co.uk/web/login.shtml")
signInButton = browser.find_element_by_css_selector(".only")
signInButton.click()
time.sleep(5)
table = browser.find_element_by_css_selector(".list-table")
for a in table.find_elements_by_tag_name("a"):
try:
if a.get_attribute("class") == "detailLink":
id = a.get_attribute("onclick")
id = id.replace("javascript:goToDetail('","")
id = id.replace("', '02260');stopEventPropagation(event);", "")
a_href = a.get_attribute("href")
browser.execute_script("window.open('about:blank','tab2');")
browser.switch_to.window("tab2")
browser.get("https://e-sourcingni.bravosolution.co.uk/esop/toolkit/opportunity/opportunityDetail.do?opportunityId="+ id +"&oppList=CURRENT")
time.sleep(2)
#wait for the element to load
browser.switch_to.window("tab1")
# print("in it ")
except:
print("detailLink is not present in the a tag class")
with open('output.csv', "w") as f:
writer = csv.writer(f)
writer.writerow(["S.No","Status","Organization","Project Title","First Publishing Date","Work Category","Listing Deadline"])
for row in table.find_elements_by_css_selector('tr'):
writer.writerow([d.text for d in row.find_elements_by_css_selector('td')])
browser.close()
添加回答
舉報