2013-06-06 123 views
0

我必须在我正在写入的python脚本(使用硒)中的字段中提取名称输入。这里是html。有没有办法获得python/selenium中另一个元素的元素?

<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 

我想在这种情况下,脚本输出“詹姆斯·布朗”。

我收到以下错误:

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

回答

1

你有没有尝试(如果你只需要文字 “詹姆斯·布朗”)

print elem1.text 

你得到错误,因为elem2elem3是字符串 - 不是硒物体 - 并且没有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 
相关问题