driver.get('https://github.com/avassalotti')
contributions = driver.find_elements_by_xpath(".//ul[@class='filter-list small']//li")
print(contributions)
for item in contributions:
print (item)
print(item.text)
item.click()
time.sleep(3)
contribution = driver.find_element_by_xpath(".//*[@class='f4 text-normal mb-2']").text
print(contribution)
程序适用于此链接(https://github.com/alex)而不适用于(https://github.com/agronholm,https://github.com/avassalotti)。
任何解决问题的建议。
要检索贡献的详细信息,避免过时的元素引用,可以使用以下解决方案:
>
代码块:
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
years = []
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_argument('disable-infobars')
driver=webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
driver.get("https://github.com/agronholm")
contributions = WebDriverWait(driver, 5).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@class='profile-timeline-year-list js-profile-timeline-year-list bg-white js-sticky float-right col-2 pl-5']/ul[@class='filter-list small']//li/a")))
for item in contributions:
print(item.get_attribute("innerHTML"))
years.append(item.get_attribute("href"))
for year in years:
driver.get(year)
print(WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='js-yearly-contributions']//h2[@class='f4 text-normal mb-2']"))).get_attribute("innerHTML"))
控制台输出:
2018
2017
2016
2015
2014
2013
2012
2011
2010
2009
260 contributions in the last year
637 contributions in 2017
770 contributions in 2016
298 contributions in 2015
239 contributions in 2014
101 contributions in 2013
113 contributions in 2012
90 contributions in 2011
16 contributions in 2010
2 contributions in 2009
我正在尝试从网站获取剧集链接。所以我需要进入链接,从剧集中获取信息。它在2-3集中工作,然后在底部因错误而崩溃。我试着提高睡眠时间,但还是失败了。 错误:
我的程序抛出一条错误消息“stale element reference:element未附加到页面文档”。当我查看前面的帖子(如Python Selenium陈旧元素修复)时,我发现我没有在调用click函数后更新url。我更新了网址。然而,它并没有解决这个问题。谁能指出我哪里出错了?下面是我的代码:
这是HTML结构如下所示 错误跟踪为:
我在我的selenium java项目中不断收到一条错误消息。
我正在尝试读取联系人页面中的数据,但遇到此异常。