[英]How to click this button on this URL using selenium and Python?
[英]Python - How to print URL using .click/selenium
我的代碼進入網頁,可以點擊頁面的每一行。
我想最終將每一行的每個鏈接打開為一個新頁面,這樣我就可以保存每個頁面的 URL 。
如果沒有 Seleniums.click()(我相信),我無法獲得 URL(我相信),所以我求助於.click,然后獲得 currentURL。
此代碼僅適用於第一行,因為網頁退出原始 URL。
有沒有更有效的方法來打印每行的 URL?
from selenium import webdriver
import time
sam=[]
import pandas as pd
driver = webdriver.Chrome()
for x in range (1,8):
driver.get(f'https://www.abstractsonline.com/pp8/#!/9325/presentations/endometrial/{x}')
time.sleep(3)
page_source = driver.page_source
eachrow=driver.find_elements_by_xpath('//*[@id="results"]/li')
for item in eachrow:
oth = item.find_element_by_xpath(".//h1[@class='name']")
oth.click()
print(driver.current_url)
<h1 class="name" data-id="696" data-key="">
<span class="title color-primary">
<i class="icon-caret-right"></i>
<span class="bodyTitle">Panel Discussion</span>
</span>
</h1>
您的每個元素都有一個數據 ID 抓取所有元素,只有 append url
https://www.abstractsonline.com/pp8/#!/9325/presentation/696
https://www.abstractsonline.com/pp8/#!/9325/presentation/{}
然后,您可以通過
eachrow=["https://www.abstractsonline.com/pp8/#!/9325/presentation/"+x.get_attribute('data-id') for x in driver.find_elements_by_xpath('//*[@id="results"]/li//h1[@class='name']')]
for row in eachrow:
driver.get(row)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.