我的问题目标:获取候选人的姓名(这是HTML表格的一部分)并将其写入文件。
(我的整个计划,在第2点,目标是:在网站上输入注册号,点击提交,获取候选人的姓名(这是问题),将注册号码与名称放在一起文件,然后返回并重复相同,直到最后一个数字。) 该表如下所示:HTML TABLE IN THE SITE
<table id="details" class="table">
<tbody>
<tr>
<td width="15%">Name</td>
<td width="85%" colspan="3"><span style="font-weight: bold"> ANILKUMAR </span></td>
</tr>
<tr>
<td>Reg. No.</td>
<td colspan="3"><span style="font-weight: bold"> 461684</span></td>
</tr>
</tbody>
</table>
&#13;
2.我的 PYTHON 代码,根据目标指定,
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import pyautogui,pyperclip
#Function.
def fun1(roll_no):
while(i<999999):
#INPUT: Put's the number in text bar, in Home Page, then clicks "Submit"
inputElement = browser.find_element_by_id("reg")
inputElement.send_keys(roll_no)
inputElement.send_keys(Keys.ENTER)
#SNIPPET TO GET THE NAME OF CANDIDATE, HOW?
#fs.write(str(i)+'\t'+name+'\n') , to WRITE INTO FILE.
#'name' in above line refers to the candidate's name, a string.
#OUT: Click's the "Back" button to go back to Home Page.
outputElement = browser.find_element_by_link_text('Back')
outputElement.click()
roll_no = roll_no + 1
#MAIN MODULE
#OPENS Firefox using selenium webdriver
browser = webdriver.Firefox()
#The intended "Home Page"
browser.get("http://karresults.nic.in/indexpuc_2016.asp")
roll_no=110000 #Starting Roll Number.
#File open for writing.
fs=open("testfile.txt","w")
#call the function
fun1(i)
答案 0 :(得分:1)
您可以使用find_elements_by_xpath()使用以下xpath查找包含文本Name的元素的所有兄弟节点:
list = driver.find_elements_by_xpath("td[text()='Name']/following-sibling::td/span")
for element in list:
print element.text