Selenium Python: execute_script 循环问题
Selenium Python: execute_script in a loop problem
我想 运行 execute_script 循环获取网站的详细信息。
这是我的完整代码:
import requests
from selenium import webdriver
from lxml import html
from urllib2 import urlopen
from bs4 import BeautifulSoup
import pandas as pd
import time
driver = webdriver.Chrome('C:\Users\Administrator\Documents\chromedriver.exe')
driver.get("https://www.ffbatiment.fr/federation-francaise-du-batiment/laffb/annuaire.html?Typ=2&Dep=03&Acti=724&Comm=0")
button = driver.find_element_by_id('ctl00_ctl00_ContentPlaceHolderGlobal_ContentPlaceHolderContenu_Button1')
href = button.get_attribute("href")
driver.execute_script(href, button)
listdata = driver.find_elements_by_xpath('//tr[@class="rgRow"]/td[1]/a')
for i in listdata:
href = i.get_attribute("href")
driver.execute_script(href, i)
if(href):
page_source = driver.page_source
soup = BeautifulSoup(page_source, 'lxml')
name = soup.find('span', {"id": "ctl00_ctl00_ContentPlaceHolderGlobal_ContentPlaceHolderContenu_FormView1_Ent_NomLabel"}).get_text()
address = soup.find('span', {"id": "ctl00_ctl00_ContentPlaceHolderGlobal_ContentPlaceHolderContenu_FormView1_Ent_AdresseLabel"}).get_text()
print(name)
print(address)
backbutton = driver.find_element_by_id('ctl00_ctl00_ContentPlaceHolderGlobal_ContentPlaceHolderContenu_FormView1_LButtonRetour')
href2 = backbutton.get_attribute("href")
driver.execute_script(href2, backbutton)
time.sleep(5)
打印姓名和地址成功,但 execute_script 对下一个循环不起作用。
错误:
StaleElementReferenceExceptionTraceback (most recent call last)
<ipython-input-19-67f221094497> in <module>()
16
17 for i in listdata:
---> 18 href = i.get_attribute("href")
19 driver.execute_script(href, i)
20 if(href):
......
StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
(Session info: chrome=83.0.4103.116)
谢谢。
试试这个代码:
import requests
from selenium import webdriver
from lxml import html
from urllib2 import urlopen
from bs4 import BeautifulSoup
import pandas as pd
import time
driver = webdriver.Chrome('chromedriver.exe')
driver.get("https://www.ffbatiment.fr/federation-francaise-du-batiment/laffb/annuaire.html?Typ=2&Dep=03&Acti=724&Comm=0")
button = driver.find_element_by_id('ctl00_ctl00_ContentPlaceHolderGlobal_ContentPlaceHolderContenu_Button1')
href = button.get_attribute("href")
driver.execute_script(href, button)
listdata = driver.find_elements_by_xpath('//tr[@class="rgRow"]/td[1]/a')
for i in range(len(listdata)):
temp = driver.find_elements_by_xpath('//tr[@class="rgRow"]/td[1]/a')
href = temp[i].get_attribute("href")
driver.execute_script(href, temp[i])
if(href):
page_source = driver.page_source
soup = BeautifulSoup(page_source, 'lxml')
name = soup.find('span', {"id": "ctl00_ctl00_ContentPlaceHolderGlobal_ContentPlaceHolderContenu_FormView1_Ent_NomLabel"}).get_text()
address = soup.find('span', {"id": "ctl00_ctl00_ContentPlaceHolderGlobal_ContentPlaceHolderContenu_FormView1_Ent_AdresseLabel"}).get_text()
print(name)
print(address)
backbutton = driver.find_element_by_id('ctl00_ctl00_ContentPlaceHolderGlobal_ContentPlaceHolderContenu_FormView1_LButtonRetour')
href2 = backbutton.get_attribute("href")
driver.execute_script(href2, backbutton)
time.sleep(5)
我想 运行 execute_script 循环获取网站的详细信息。
这是我的完整代码:
import requests
from selenium import webdriver
from lxml import html
from urllib2 import urlopen
from bs4 import BeautifulSoup
import pandas as pd
import time
driver = webdriver.Chrome('C:\Users\Administrator\Documents\chromedriver.exe')
driver.get("https://www.ffbatiment.fr/federation-francaise-du-batiment/laffb/annuaire.html?Typ=2&Dep=03&Acti=724&Comm=0")
button = driver.find_element_by_id('ctl00_ctl00_ContentPlaceHolderGlobal_ContentPlaceHolderContenu_Button1')
href = button.get_attribute("href")
driver.execute_script(href, button)
listdata = driver.find_elements_by_xpath('//tr[@class="rgRow"]/td[1]/a')
for i in listdata:
href = i.get_attribute("href")
driver.execute_script(href, i)
if(href):
page_source = driver.page_source
soup = BeautifulSoup(page_source, 'lxml')
name = soup.find('span', {"id": "ctl00_ctl00_ContentPlaceHolderGlobal_ContentPlaceHolderContenu_FormView1_Ent_NomLabel"}).get_text()
address = soup.find('span', {"id": "ctl00_ctl00_ContentPlaceHolderGlobal_ContentPlaceHolderContenu_FormView1_Ent_AdresseLabel"}).get_text()
print(name)
print(address)
backbutton = driver.find_element_by_id('ctl00_ctl00_ContentPlaceHolderGlobal_ContentPlaceHolderContenu_FormView1_LButtonRetour')
href2 = backbutton.get_attribute("href")
driver.execute_script(href2, backbutton)
time.sleep(5)
打印姓名和地址成功,但 execute_script 对下一个循环不起作用。
错误:
StaleElementReferenceExceptionTraceback (most recent call last)
<ipython-input-19-67f221094497> in <module>()
16
17 for i in listdata:
---> 18 href = i.get_attribute("href")
19 driver.execute_script(href, i)
20 if(href):
......
StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
(Session info: chrome=83.0.4103.116)
谢谢。
试试这个代码:
import requests
from selenium import webdriver
from lxml import html
from urllib2 import urlopen
from bs4 import BeautifulSoup
import pandas as pd
import time
driver = webdriver.Chrome('chromedriver.exe')
driver.get("https://www.ffbatiment.fr/federation-francaise-du-batiment/laffb/annuaire.html?Typ=2&Dep=03&Acti=724&Comm=0")
button = driver.find_element_by_id('ctl00_ctl00_ContentPlaceHolderGlobal_ContentPlaceHolderContenu_Button1')
href = button.get_attribute("href")
driver.execute_script(href, button)
listdata = driver.find_elements_by_xpath('//tr[@class="rgRow"]/td[1]/a')
for i in range(len(listdata)):
temp = driver.find_elements_by_xpath('//tr[@class="rgRow"]/td[1]/a')
href = temp[i].get_attribute("href")
driver.execute_script(href, temp[i])
if(href):
page_source = driver.page_source
soup = BeautifulSoup(page_source, 'lxml')
name = soup.find('span', {"id": "ctl00_ctl00_ContentPlaceHolderGlobal_ContentPlaceHolderContenu_FormView1_Ent_NomLabel"}).get_text()
address = soup.find('span', {"id": "ctl00_ctl00_ContentPlaceHolderGlobal_ContentPlaceHolderContenu_FormView1_Ent_AdresseLabel"}).get_text()
print(name)
print(address)
backbutton = driver.find_element_by_id('ctl00_ctl00_ContentPlaceHolderGlobal_ContentPlaceHolderContenu_FormView1_LButtonRetour')
href2 = backbutton.get_attribute("href")
driver.execute_script(href2, backbutton)
time.sleep(5)