如何在 XPath 表达式中使用 Python 变量? [英] How to use Python variable in an XPath expression?
问题描述
我试图从这个网站 https://www.quora.com/profile/Karan-Bansal-3/followers
I am trying to get all the followers name
from this website https://www.quora.com/profile/Karan-Bansal-3/followers
由于不是一次加载整个页面,我每次都在循环中使用它:
Since the whole page is not loaded at once, I am using this everytime in a loop :
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
现在由于我无法一次选择所有元素,我尝试使用索引来查找循环中的元素.
Now since I can't select all the element at once, I am trying to use indexing to find the element in the loop.
people = driver.find_element_by_xpath("//div[@class='pagedlist_item'][i]/*/div[@class='ObjectCard-header']/a[@class='user']")
这里如您所见,我正在尝试使用 [i]
提供索引,这显然不起作用并代替它,如果我提供 [1] 或任何有效的数字好.那么如何才能一一选择元素.
Here as you can see, I am trying to give the indexing using [i]
which clearly doesn't work and in place of it, if I give [1] or any number it works well. So how can I select the element one by one.
代码片段:
i=1
target = open(filename,'w')
driver.get('https://www.quora.com/profile/Karan-Bansal-3/followers')
while True:
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
people = driver.find_element_by_xpath("//div[@class='pagedlist_item'][i]/*/div[@class='ObjectCard-header']/a[@class='user']")
target.write(people.text)
target.write("\n")
i = i+1
推荐答案
在宿主语言中使用字符串连接,以便在构造 XPath 之前评估 i
.否则,[i]
是对 i
元素存在的谓词测试.您没有说明您的托管语言是什么,但假设字符串连接是 "string" + "string"
:
Use string concatenation in the hosting language so that i
is evaluated before constructing the XPath. Otherwise, [i]
is a predicate testing for the presence of an i
element. You didn't state what your hosting language is, but assuming string concatenation is "string" + "string"
:
"//div[@class='pagedlist_item'][" + i + "]/*/div[@class='ObjectCard-header']/a[@class='user']"
更新:好的,所以您在 Python 中托管 XPath.
Update: Ok, so you're hosting XPath in Python.
如果您首先通过 str(i)
将 i
转换为字符串,则可以使用 +
进行连接,
You can use +
to concatenate above if you first cast i
to a string via str(i)
,
"//div[@class='pagedlist_item'][" + str(i) + "]/*/div[@class='ObjectCard-header']/a[@class='user']"
或者您可以使用我提供的链接中使用的 format()
:
or you can use format()
as is used in the link I provided:
"//div[@class='pagedlist_item'][{}]/*/div[@class='ObjectCard-header']/a[@class='user']".format(i)
无论哪种方式,将上述构造的 XPath 表达式放入您对 find_element_by_xpath()
的调用中,您的问题就应该得到解决.
either way, place the above constructed XPath expressions into your call to find_element_by_xpath()
and your problem should be solved.
警告:不要将这种方法用于 i
的不可信值,否则您可以打开代码到 XPath 注入攻击.
Caution: Do not use this approach with untrusted values for i
or you could open your code to XPath injection attacks.
这篇关于如何在 XPath 表达式中使用 Python 变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!