0

我必须在我正在编写的 python 脚本的字段中提取名称输入(使用 selenium)。这是html。

<div id="CF00NC000000576hm_ileinner"><a onmouseover="LookupHoverDetail.getHover('lookup005C0000003xG8C00NC000000576hm', '/005C0000003xG8C/m?retURL=%2F00QC000001DMZB8&amp;isAjaxRequest=1&amp;nocache=1370473054922').show();" onmouseout="LookupHoverDetail.getHover('lookup005C0000003xG8C00NC000000576hm').hide();" onfocus="LookupHoverDetail.getHover('lookup005C0000003xG8C00NC000000576hm', '/005C0000003xG8C/m?retURL=%2F00QC000001DMZB8&amp;isAjaxRequest=1&amp;nocache=1370473054922').show();" onblur="LookupHoverDetail.getHover('lookup005C0000003xG8C00NC000000576hm').hide();" id="lookup005C0000003xG8C00NC000000576hm" href="/005C0000003xG8C">James Brown</a></div>

xpath 是//*[@id="CF00NC000000576hm_ileinner"].

关于如何解决这个问题,我最好的猜测是使用:

elem1 = driver.find_element_by_xpath('//*[@id="CF00NC000000576hm_ileinner"]')
elem2 = elem1.get_attribute("innerHTML")
elem3 = elem2.get_attribute("innerHTML")
print elem3

在这种情况下,我希望脚本输出“James Brown”。

我收到以下错误:

AttributeError: 'unicode' object has no attribute 'get_attribute'

4

1 回答 1

1

你试过吗(如果你只需要文本“詹姆斯布朗”)

 print elem1.text

你得到错误是因为elem2orelem3是字符串 - 不是 selenium 对象 - 并且没有get_attribute()功能。尝试:

 elem1 = driver.find_element_by_xpath('//*[@id="CF00NC000000576hm_ileinner"]')

 print elem1

 elem2 = elem1.get_attribute("innerHTML")

 print elem2

 elem3 = elem2.get_attribute("innerHTML")

 print elem3
于 2013-06-06T01:11:34.213 回答