简体   繁体   English

如何使用 Selenium WebDriver 将复制的文本存储到 Python 中的变量中

[英]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.

相关问题 如何使用 Python 在 Selenium Webdriver 中高效迭代变量数? - How to efficiently iterate variable number in Selenium Webdriver using Python? 如何使用Selenium WebDriver Python检查文本是否存在? - How to check if text is present using selenium webdriver python? 如何使用Selenium WebDriver和Python提取元素中的文本? - How to extract the text within the element using Selenium WebDriver and Python? 如何使用带有Python的Selenium Webdriver从工具提示中获取没有属性的文本? - How to get text from tooltip with no attributes using Selenium Webdriver with Python? 如何使用selenium webdriver python中的span中的定位器打印文本? - How to print the text using a locator from the span in selenium webdriver python? 如何使用Python使用Selenium Webdriver获取此html中的一些文本值? - How to get the some text value in this html with Selenium Webdriver using Python? 如何使用 Selenium WebDriver 和 Python 从表中获取元素(文本) - How to get element(text) from a table using Selenium WebDriver with Python 如何在 For 循环 Python 中使用 Selenium WebDriver 获取文本 - How to get text with Selenium WebDriver in a For Loop Python 如何在 Python 中使用 Selenium WebDriver 获取文本 - How to get text with Selenium WebDriver in Python Selenium + Python:将文本存储在变量中并打印 - Selenium + Python: Store text in a variable and print
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM