1
self.br.execute_script("document.getElementById('ctl00_phMainContent_KeywordForm_ControlPanel_txtKeywords').value = 'keywordurl';")

I use selenium in python, I have the following code which executes Javascript. The part of keyworurl is a python string list of around 1300 lines. But when I use that code it will write "keywordurl" and not what the keywordurl represents in python... any ideas? If it was possible to do " + keywordurl + " or something?

4

2 回答 2

0

可以在 python :p 中进行字符串添加。不过最好使用 str.format :

>>> print "Hello {}!".format("World")
"Hello World!"

因为听起来你有一个包含 1300 个字符串的列表,所以最好先使用 list.join 将它变成一个巨大的字符串。

>>> print "".join(["1", "2", "3"])
'123'
于 2013-11-10T23:33:20.350 回答
0

使用 Python 字符串格式化:

for keywordurl in open("textfile.txt").readlines():
    self.br.execute_script( "document.getElementById('ctl00_phMainContent_KeywordForm_ControlPanel_txtKeywords').value = '%s';" % keywordurl )

-%s说明符被替换为 - 运算符之后的字符串 %

更多详细信息:http ://docs.python.org/2/library/stdtypes.html#string-formatting

于 2013-11-10T23:35:33.627 回答