[英]How to store copied text into a variable in Python using Selenium WebDriver
I am working on some web scraping projects, where I scrape the data and store it into the CSV file.我正在处理一些网络抓取项目,在那里我抓取数据并将其存储到 CSV 文件中。 I am stuck in a case where I need to store copied the data into a variable.
我遇到了需要将复制的数据存储到变量中的情况。
Actually, the text is generated after clicking on a button and automatically copied, just I need to save this copied text into a variable.实际上,文本是在单击按钮后生成并自动复制的,只是我需要将此复制的文本保存到变量中。 Now for this, I use following commands but nothing is working.
现在为此,我使用以下命令但没有任何效果。
Command1-命令1-
spam=driver.find_element_by_xpath('//div[@data-control-name="copy_address]').click()
print("copied text",spam)
it shows "copied text none"它显示“复制文本无”
Even I used pyperclip library as well but nothing works for me.甚至我也使用了 pyperclip 库,但对我来说没有任何作用。
Command2-命令2-
pyperclip.copy(driver.find_element_by_xpath('//div[@data-control-name="copy_address"]').click())
spam = pyperclip.paste()
How can I proceed?我该如何继续?
You are clicking the element in that xpath
not copying its text, that is why your variable returns None
.您正在单击该
xpath
的元素而不是复制其文本,这就是您的变量返回None
。
Try:尝试:
spam=driver.find_element_by_xpath('//div[@data-control-name="copy_address]').text
print(spam)
If it won't work, maybe the element you are getting is not a text.如果它不起作用,也许您获得的元素不是文本。 Try getting its html code instead using the
get_attribute
method:尝试使用
get_attribute
方法获取其 html 代码:
spam=driver.find_element_by_xpath('//div[@data-control-name="copy_address]')
your_text = spam.get_attribute('outerHTML')
print(your_text)
This will give you full html of the element you're interested in, then you just have to substring the result to get the text you need.这将为您提供您感兴趣的元素的完整 html,然后您只需对结果进行子字符串化即可获得所需的文本。
Hope this helps.希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.