我试图从一个元素中获取一个列表
<a href="/Max/Project" itemprop="name codeRepository">Project</a>
xpath 是
/html/body/div[4]/main/div[2]/div/div[2]/div[2]/div/div[2]/ul/li[1]/div[1]/div[1]/h3/a
我已经试过了
ids = driver.find_elements_by_xpath("a[@id='user-repositories-list']")
但它不会显示包含此 git 用户的存储库的列表。
谢谢!
答案 0 :(得分:2)
您可以像这样检索 git 用户的存储库链接:
repos = WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@id='user-repositories-list']/ul/li/div/div/h3/a")))
repo_names = []
for repo in repos:
href = repo.get_attribute("href")
href = href.rsplit('/', 1)[1]
repo_names.append(href)
print(repo_names)
然后,您只需要进行一些文本解析即可从链接中提取存储库名称。
注意 html 的嵌套结构。在 id='user-repositories-list' 下,可以找到存储库的所有块。 h3
代表三阶标题,a
标记表示超链接。
WebDriverWait 确保等待所有元素都可见并可以找到。