我正在尝试使用Selenium从网页中提取IP地址信息,这里是代码的一部分
removals = []
ServerNames = driver.find_elements_by_xpath("//span[contains(text(), 'Microsoft') or contains(text(), 'Solaris') or contains(text(), 'Linux') or contains(text(), 'Server') or contains(text(), 'Windows') or contains(text(), 'AIX') or contains(text(), 'VMware') or contains(text(), 'ESX')]")
Backups = driver.find_elements_by_xpath("//span[contains(text(), '|locationInherited=true')]")
ips = driver.find_elements_by_xpath("//span[contains(text(), '10.') or contains(text(), '167.') or contains(text(), '170.') or contains(text(), '192.') or contains(text(), '172.') or contains(text(), '204.')]")
for x, y in itertools.zip_longest(ServerNames, Backups, fillvalue=''):
x.click()
ips
y.click()
ips
for ip in ips:
removals.append(ip.text)
当找到并单击“ServerNames”对象时,该网页将显示该特定服务器的主要和备用IP地址,并说明需要脚本:
1)遍历每个找到的“ServerNames”对象 2)单击对象 3)获取相关的主IP 4)单击找到的关联“备份”对象 5)抓住那个IP
然后转到下一个ServerNames对象并重复这些步骤,直到提取了所有服务器的所有IP地址。我知道driver.find_elements方法基本上是创建列表,所以基本上我需要一种方法来交替点击&通过列表提取每次迭代的数据。谢谢,
使用上面的代码我得到了这个错误:
c:\Python35\Scripts>python Remedy2.py
Traceback (most recent call last):
File "Remedy2.py", line 73, in <module>
y.click()
AttributeError: 'str' object has no attribute 'click'
使用此代码:
for x in ServerNames:
x.click()
ips = driver.find_elements_by_xpath("//span[contains(text(), '10.') or contains(text(), '167.') or contains(text(), '170.') or contains(text(), '192.') or contains(text(), '172.') or contains(text(), '204.')]")
if Backups:
for y in Backups:
y.click()
ips = driver.find_elements_by_xpath("//span[contains(text(), '10.') or contains(text(), '167.') or contains(text(), '170.') or contains(text(), '192.') or contains(text(), '172.') or contains(text(), '204.')]")
我收到'陈旧元素错误'
答案 0 :(得分:1)
如果页面在操作后发生更改,则必须再次找到该元素。引发陈旧元素erorr可能是因为该元素不再附加到DOM。方法如下:
for i in range(len(ServerNames)):
server = driver.find_elements_by_xpath("//span[contains(text(), 'Microsoft') or contains(text(), 'Solaris') or contains(text(), 'Linux') or contains(text(), 'Server') or contains(text(), 'Windows') or contains(text(), 'AIX') or contains(text(), 'VMware') or contains(text(), 'ESX')]")[i]
server.click()
ips = driver.find_elements_by_xpath("//span[contains(text(), '10.') or contains(text(), '167.') or contains(text(), '170.') or contains(text(), '192.') or contains(text(), '172.') or contains(text(), '204.')]")
if Backups:
for j in range(len(Backups)):
backup = driver.find_elements_by_xpath("//span[contains(text(), '|locationInherited=true')]")[j]
backup.click()
ips = driver.find_elements_by_xpath("//span[contains(text(), '10.') or contains(text(), '167.') or contains(text(), '170.') or contains(text(), '192.') or contains(text(), '172.') or contains(text(), '204.')]"