获取由 Selenium Python 中的 Html 按钮生成的内容
get content produced by Html button in Selenium Python
使用 Selenium 我正在尝试读取 "a" 标签中包含的 href link 的内容,它是 Quora 上的 "View Upvoter" 按钮(为您提供投票者用户名列表),我我在 selenium 中使用这个 python 代码,但我得到的结果总是空的,有什么建议吗?
这是 python 代码:
inputElement = browser.find_element_by_class_name("VoterListModalLink")
inputElement.send_keys("\n") #send enter for links, bttons
print(inputElement.text)
这是 quora 按钮 html :
<a class="AnswerVoterListModalLink VoterListModalLink"
href="/api /mobile_expanded_voter_list?key=zDDQQihxghH&type=answer"
id="__w2_iXtwcOw_modal_link" target="_blank">View Upvoters
</ a>
试试这个 Get value of an input box using Selenium (Python) :
inputElement.get_attribute('value')
您与我们分享的 link 中有 5 View Upvoters link。
要从 DOM 中检索 href 属性,您可以先将 5 个 Web 元素存储在一个列表中,然后使用 for each 循环来提取所需的属性。
代码 :
driver.get("https://www.quora.com/Can-you-cash-out-bitcoins")
upvoter_list = driver.find_elements_by_link_text('View Upvoters')
print(len(upvoter_list))
for voter in upvoter_list:
print(voter.get_attribute('href'))
更新 1:
driver.maximize_window()
driver.get("https://www.quora.com/Can-you-cash-out-bitcoins")
wait = WebDriverWait(driver, 10)
upvoter_list = driver.find_elements_by_link_text('View Upvoters')
print(len(upvoter_list))
#Clicking on first upvoter list
upvoter_list[0].click()
time.sleep(10)
#Now we will get all the names from newly opened pop up
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'div.modal_content.modal_body')))
upvoter_name = wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, 'span.feed_item_answer_user')))
print(len(upvoter_name))
for names in upvoter_name:
print(names.text)
控制台输出:
Jameson Williams
Fay M
David Robert
James Lathrom
James Newman
Debessence G
Valeriy Velchev
Chris Wright
BA Psychology, The University of Britis
Stanley Armando
Amisi Omomdi
Studies at The United States of America
Sathish Dilli Mohan
Just like you!
Alberto Guzman
Spanish-English Interpreter
Stephen C. Liu
Chief Matchmaker at M8 Relationship Mat
Ranieri Mestroni
Works at Parlanta Corp
Clasina Bos
Faryal Baloch
RheA Wolbrink
Krish Munot
Studied at Anna University, Tamil Nadu,
Heiko Yeh
Intern UX Design at Otto
David Jockers
Iddrisu Sadik Debabs
Works at Debabs Inc
Arkadiusz Hukalowicz
Joel Dubow
Sanjeev Kumar
Self Employed at Share Market India (19
Sandy
Wilfred Lee
Works at Investment Banking
Peter Yeung
Michael Donahue
SYED JALAL
Joshua Stockton
Works at University of Phoenix
Max Gauth
Dejan Cvetkovic
Joseph "Alec" Sidwa
Worked at Eastman Kodak Products and Se
Stavros Asimakopoulos
Vijay Paramasivam Rajasekaran
Studied at Jayaram College of Engineeri
Bitoshi Sakamoto
Girish Illindram
Engineer at Kuwait Oil Company (2016-pr
Faliq Nordin
Yamil Munoz
Worked at Odebrecht
Wesley Knope
I own some
Vinay Kumar
Works at Sonata Software
Jack Vi
Tony Aidinis
Project Manager at CryptoRose Analytics
Renee Bahman
Apoorv Jain
Bitcoin and alt coins trading.
Ashton Simonek
Studies Investing & Business at Self-Te
Chris Kelly
Cathy Young Brown
LDS, Married 42 years, Mom of 5 & Nana
Jonas Grandt
Jun Rong Tan
Seth Benton
M.S. from Arizona State University (200
José Romero
Worked at Universidad Nacional Abierta
Marijn Edens
Studied at KU Leuven
Mehmet Bal
Software Engineer
Stan Marian
Alan Morrison
Sr. Research Fellow at PricewaterhouseC
Jay Jackson
Jacob Hood
Assembly Line Worker at Honda Of Canada
Dawei Chen
Worked at University of Southern Califo
Jeffrey Tyson
Elliott Wells
Former Real Estate Investor
Sid Maher
Director at Casiki Media Ltd (2017-pres
Adithya G Kamath
Pritam Garud
Works at Tata Motors
Bence Mitlasóczki
M.Sc Physics, University of Bonn (2019)
希望对您有所帮助
使用 Selenium 我正在尝试读取 "a" 标签中包含的 href link 的内容,它是 Quora 上的 "View Upvoter" 按钮(为您提供投票者用户名列表),我我在 selenium 中使用这个 python 代码,但我得到的结果总是空的,有什么建议吗? 这是 python 代码:
inputElement = browser.find_element_by_class_name("VoterListModalLink")
inputElement.send_keys("\n") #send enter for links, bttons
print(inputElement.text)
这是 quora 按钮 html :
<a class="AnswerVoterListModalLink VoterListModalLink"
href="/api /mobile_expanded_voter_list?key=zDDQQihxghH&type=answer"
id="__w2_iXtwcOw_modal_link" target="_blank">View Upvoters
</ a>
试试这个 Get value of an input box using Selenium (Python) :
inputElement.get_attribute('value')
您与我们分享的 link 中有 5 View Upvoters link。
要从 DOM 中检索 href 属性,您可以先将 5 个 Web 元素存储在一个列表中,然后使用 for each 循环来提取所需的属性。
代码 :
driver.get("https://www.quora.com/Can-you-cash-out-bitcoins")
upvoter_list = driver.find_elements_by_link_text('View Upvoters')
print(len(upvoter_list))
for voter in upvoter_list:
print(voter.get_attribute('href'))
更新 1:
driver.maximize_window()
driver.get("https://www.quora.com/Can-you-cash-out-bitcoins")
wait = WebDriverWait(driver, 10)
upvoter_list = driver.find_elements_by_link_text('View Upvoters')
print(len(upvoter_list))
#Clicking on first upvoter list
upvoter_list[0].click()
time.sleep(10)
#Now we will get all the names from newly opened pop up
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'div.modal_content.modal_body')))
upvoter_name = wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, 'span.feed_item_answer_user')))
print(len(upvoter_name))
for names in upvoter_name:
print(names.text)
控制台输出:
Jameson Williams
Fay M
David Robert
James Lathrom
James Newman
Debessence G
Valeriy Velchev
Chris Wright
BA Psychology, The University of Britis
Stanley Armando
Amisi Omomdi
Studies at The United States of America
Sathish Dilli Mohan
Just like you!
Alberto Guzman
Spanish-English Interpreter
Stephen C. Liu
Chief Matchmaker at M8 Relationship Mat
Ranieri Mestroni
Works at Parlanta Corp
Clasina Bos
Faryal Baloch
RheA Wolbrink
Krish Munot
Studied at Anna University, Tamil Nadu,
Heiko Yeh
Intern UX Design at Otto
David Jockers
Iddrisu Sadik Debabs
Works at Debabs Inc
Arkadiusz Hukalowicz
Joel Dubow
Sanjeev Kumar
Self Employed at Share Market India (19
Sandy
Wilfred Lee
Works at Investment Banking
Peter Yeung
Michael Donahue
SYED JALAL
Joshua Stockton
Works at University of Phoenix
Max Gauth
Dejan Cvetkovic
Joseph "Alec" Sidwa
Worked at Eastman Kodak Products and Se
Stavros Asimakopoulos
Vijay Paramasivam Rajasekaran
Studied at Jayaram College of Engineeri
Bitoshi Sakamoto
Girish Illindram
Engineer at Kuwait Oil Company (2016-pr
Faliq Nordin
Yamil Munoz
Worked at Odebrecht
Wesley Knope
I own some
Vinay Kumar
Works at Sonata Software
Jack Vi
Tony Aidinis
Project Manager at CryptoRose Analytics
Renee Bahman
Apoorv Jain
Bitcoin and alt coins trading.
Ashton Simonek
Studies Investing & Business at Self-Te
Chris Kelly
Cathy Young Brown
LDS, Married 42 years, Mom of 5 & Nana
Jonas Grandt
Jun Rong Tan
Seth Benton
M.S. from Arizona State University (200
José Romero
Worked at Universidad Nacional Abierta
Marijn Edens
Studied at KU Leuven
Mehmet Bal
Software Engineer
Stan Marian
Alan Morrison
Sr. Research Fellow at PricewaterhouseC
Jay Jackson
Jacob Hood
Assembly Line Worker at Honda Of Canada
Dawei Chen
Worked at University of Southern Califo
Jeffrey Tyson
Elliott Wells
Former Real Estate Investor
Sid Maher
Director at Casiki Media Ltd (2017-pres
Adithya G Kamath
Pritam Garud
Works at Tata Motors
Bence Mitlasóczki
M.Sc Physics, University of Bonn (2019)
希望对您有所帮助