3 回答

TA贡献1868条经验 获得超4个赞
我不使用 twitter,所以我不完全知道你在说什么搜索框,但如果你只想在搜索框中输入一些文本并按 Enter,那么,将其替换为:
# Enter text in the search box
self.driver.find_element_by_xpath('//*[@id="react-root"]/div/div/div[2]/main/div/div/div/div[2]/div/div[2]/div/div/div/div[1]/div/div/div/form/div[1]/div/div/div[2]/input').send_keys(search_text)
search_text.send_keys(Keys.ENTER)
有了这个:
# Enter text in the search box
element = self.driver.find_element_by_xpath('//*[@id="react-root"]/div/div/div[2]/main/div/div/div/div[2]/div/div[2]/div/div/div/div[1]/div/div/div/form/div[1]/div/div/div[2]/input')
element.send_keys(search_text)
element.send_keys(Keys.ENTER)
我无法在我的机器上测试这个,因为我不使用 Twitter,但我认为它应该可以工作。请让我知道这可不可以帮你。谢谢

TA贡献1810条经验 获得超5个赞
该.send_keys(...)方法属于WebElement,而不是字符串。
这就是导致您的代码产生此错误的原因:
AttributeError:“str”对象没有属性“send_keys”
而不是这一行:
# Enter text in the search box
self.driver.find_element_by_xpath('//*[@id="react-root"]/div/div/div[2]/main/div/div/div/div[2]/div/div[2]/div/div/div/div[1]/div/div/div/form/div[1]/div/div/div[2]/input')\
.send_keys(search_text)
search_text.send_keys(Keys.ENTER)
您可以使用以下代码进行更改:
search_box = self.driver.find_element_by_xpath('//*[@id="react-root"]/div/div/div[2]/main/div/div/div/div[2]/div/div[2]/div/div/div/div[1]/div/div/div/form/div[1]/div/div/div[2]/input')
search_box.send_keys(search_text)
search_box.send_keys(Keys.ENTER)
您应该初始化search_box为WebElement,输入文本,然后使用回车键提交。

TA贡献2037条经验 获得超6个赞
这个错误信息...
AttributeError: 'str' object has no attribute 'send_keys'
...意味着您的脚本/程序已尝试调用send_keys()对象string。
什么地方出了错
根据代码行:
search_text.send_keys(Keys.ENTER)
您正在尝试调用传递给方法的字符串类型send_keys()变量。其中 as是与WebElement关联的方法。因此你会看到错误。search_textdef __init__(self,username, password, search_text)send_keys()
解决方案
您需要按如下方式调用 WebElementsend_keys():
self.element = self.driver.find_element_by_xpath('//*[@id="react-root"]/div/div/div[2]/main/div/div/div/div[2]/div/div[2]/div/div/div/div[1]/div/div/div/form/div[1]/div/div/div[2]/input')
self.element.send_keys(search_text)
self.element.send_keys(Keys.ENTER)
添加回答
举报