如何在python中为selenium webdriver脚本增加循环中的Xpath变量值?
search_result1 = sel.find_element_by_xpath("//a[not((//div[contains(@class,'s')]//div[contains(@class,'kv')]//cite)[1])]|((//div[contains(@class,'s')]//div[contains(@class,'kv')]//cite)[1])").text
search_result2 = sel.find_element_by_xpath("//a[not((//div[contains(@class,'s')]//div[contains(@class,'kv')]//cite)[2])]|((//div[contains(@class,'s')]//div[contains(@class,'kv')]//cite)[2])").text
search_result3 = sel.find_element_by_xpath("//a[not((//div[contains(@class,'s')]//div[contains(@class,'kv')]//cite)[3])]|((//div[contains(@class,'s')]//div[contains(@class,'kv')]//cite)[3])").text
答案 0 :(得分:0)
为什么不创建用于存储类似于
的搜索结果的列表search_results=[]
for i in range(1,11) #I am assuming 10 results in a page so you can set your own range
result=sel.find_element_by_xpath("//a[not((//div[contains(@class,'s')]//div[contains(@class,'kv')]//cite)[%s])]|((//div[contains(@class,'s')]//div[contains(@class,'kv')]//cite)[%s])"%(i,i)).text
search_results.append(result)
此示例代码将创建10个结果值的列表。你可以从这段代码中得到自己的想法。它只是自动化任务的问题。
所以
search_results[0]
将为您提供第一个搜索结果
search_results[1]
将为您提供第二个搜索结果
...
...
search_results[9]
将为您提供第10个搜索结果
答案 1 :(得分:0)
search_results=[]
result_elements = sel.find_elements_by_xpath("//not/indexed/xpath/for/any/search/result")
for element in result_elements:
search_result = element.text
search_results.append(search_result)