Python中的Selenium如何在u中通过u元素来查找Selenium

2024-04-19 02:38:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要在下面的行中做一些更改:

driver.find_element_by_css_selector("p[title=\"sanityapp5\"]").click()

现在我创建了一个字符串变量appname

^{pr2}$

现在我想知道如何在上面的selenium命令中将santiyapp5替换为变量appname。 我是python新手,所以我知道怎么做。在


Tags: 字符串命令bytitledriverseleniumelementfind
2条回答

driver.find_element_by_css_selector("p[title=\"%s\"]" % appname).click()

或使用较新的样式字符串格式:

driver.find_element_by_css_selector("p[title=\{}\"]".format(appname)).click()

让我们用一种更像Python的方式来做。使用format函数。在

Python 2.7.6 (default, Mar 22 2014, 22:59:56) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> "hello world{}".format("end")
'hello worldend'
>>> "hello world {1} and {0}".format("a", "b")
'hello world b and a'
>>> 

你的情况也是如此

^{pr2}$

很少有reasons为什么你喜欢格式而不是百分比。在

相关问题 更多 >