How to use JavascriptExecutor instead of send_keys in Python selenium - python

I have written a script in python using send_key to type some text in a textarea on this webpage. However, it is really slow to use send_key as my text is really chunky.
from selenium import webdriver
text = "gckugcgaygartty"
link_url ="http://www.bioinformatics.org/sms2/translate.html"
driver = webdriver.Chrome('chromedriver', chrome_options=options)
driver.get(link_url)
driver.find_element_by_tag_name("textarea").clear()
driver.find_element_by_tag_name("textarea").send_keys("gckugcgaygartty")
I then tried to replace the send_keys with execute_script() like following but it didn't work (no errors but nothing changed on the webpage), could anyone give me some advice please?
driver.execute_script("document.getElementById('main_form').getElementsByTagName('textarea')[0].click()")
driver.execute_script("document.getElementById('main_form').getElementsByTagName('textarea')[0].setAttribute('value', 'gckugcgaygartty' )")

Modification : Changed setAttribute function with value property
Use following Code :
driver.execute_script("document.getElementsByTagName('textarea')[0].value='your_lengthy_data'")
OR
driver.execute_script("document.getElementById('main_form').getElementsByTagName('textarea')[0].value='your_lengthy_data'")

Related

Saving selenium web elements into a file

I am trying to save selenium web elements into a separate file for later use in my program to find that element easily on the same website. I tried with pickle and continuously I am getting the following error.
AttributeError: Can't pickle local object '_createenviron.<locals>.encodekey'
Tried setting the local variable into global, but still no luck.
Here is an example code to demonstrate what I am trying to achieve.
import pickle
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get('http://website.com/')
anchors = driver.find_elements(By.TAG_NAME, 'a')
for anchor in anchors:
with open('anchors_pickle', 'wb') as pick_file:
pickle.dump(anchor, pick_file) # Error
Since with JSON also this is not possible, I guess pickle is the best solution for this. Is there any method to overcome this error?

I want to merge 2 tabs into 1 page with using Selenium Python.Concretely,each page has just 1 tab and the result page has 2 tabs

I wrote below code.
from selenium import webdriver
driver1 = webdriver.Chrome()
driver1.get('https://www.google.com/')
driver2 = webdriver.Chrome()
driver2.get('https://www.yahoo.com/')
Of course this code finally create 2 pages but I want to merge it into 1 page.
How do I implement it?.
You mean you want multiple tabs in one window?
I believe there's no such function in Selenium. However, you can run JavaScript to open a URL in a new tab using the execute_script() function.
driver.get("https://google.com")
script = "window.open('https://www.yahoo.com/','_blank');"
driver.execute_script(script)
Hope this helps.

Selenium take element screenshot - Google Chrome

I'm trying to take screenshot of a <canvas> tag element from google chrome using selenium web driver and python.
I tried using the below code,
driver.find_element_by_css('#canvas-xyz').save_screenshot('canvas.png')
It returned
AttributeError: 'WebElement' object has no attribute 'save_screenshot'
I also tried this in dev tools,
document.querySelector('#canvas-xyz').toDataURL()
It returned the following DATA URI, which is empty.
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABq8AAAAhCAYAAABHnTGeAAAEQ0lEQVR4Xu3ZMREAAAwCseLfdG38kCrgUjZ2jgABAgQIECBAgAABAgQIECBAgAABAgQIECBAgEBEYJEcYhAgQIAAAQIECBAgQIAAAQIECBAgQIAAAQIECBA445USECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIZASMV5lXCEKAAAECBAgQIECAAAECBAgQIECAAAECBAgQIGC80gECBAgQIECAAAECBAgQIECAAAECBAgQIECAAIGMgPEq8wpBCBAgQIAAAQIECBAgQIAAAQIECBAgQIAAAQIEjFc6QIAAAQIECBAgQIAAAQIECBAgQIAAAQIECBAgkBEwXmVeIQgBAgQIECBAgAABAgQIECBAgAABAgQIECBAgIDxSgcIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQyAsarzCsEIUCAAAECBAgQIECAAAECBAgQIECAAAECBAgQMF7pAAECBAgQIECAAAECBAgQIECAAAECBAgQIECAQEbAeJV5hSAECBAgQIAAAQIECBAgQIAAAQIECBAgQIAAAQLGKx0gQIAAAQIECBAgQIAAAQIECBAgQIAAAQIECBDICBivMq8QhAABAgQIECBAgAABAgQIECBAgAABAgQIECBAwHilAwQIECBAgAABAgQIECBAgAABAgQIECBAgAABAhkB41XmFYIQIECAAAECBAgQIECAAAECBAgQIECAAAECBAgYr3SAAAECBAgQIECAAAECBAgQIECAAAECBAgQIEAgI2C8yrxCEAIECBAgQIAAAQIECBAgQIAAAQIECBAgQIAAAeOVDhAgQIAAAQIECBAgQIAAAQIECBAgQIAAAQIECGQEjFeZVwhCgAABAgQIECBAgAABAgQIECBAgAABAgQIECBgvNIBAgQIECBAgAABAgQIECBAgAABAgQIECBAgACBjIDxKvMKQQgQIECAAAECBAgQIECAAAECBAgQIECAAAECBIxXOkCAAAECBAgQIECAAAECBAgQIECAAAECBAgQIJARMF5lXiEIAQIECBAgQIAAAQIECBAgQIAAAQIECBAgQICA8UoHCBAgQIAAAQIECBAgQIAAAQIECBAgQIAAAQIEMgLGq8wrBCFAgAABAgQIECBAgAABAgQIECBAgAABAgQIEDBe6QABAgQIECBAgAABAgQIECBAgAABAgQIECBAgEBGwHiVeYUgBAgQIECAAAECBAgQIECAAAECBAgQIECAAAECxisdIECAAAECBAgQIECAAAECBAgQIECAAAECBAgQyAgYrzKvEIQAAQIECBAgQIAAAQIECBAgQIAAAQIECBAgQMB4pQMECBAgQIAAAQIECBAgQIAAAQIECBAgQIAAAQIZAeNV5hWCECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIGK90gAABAgQIECBAgAABAgQIECBAgAABAgQIECBAICPwDiwAIhIPZYcAAAAASUVORK5CYII=
Is it possible to take screenshot of an element using chromedriver and selenium in python. I am aware that chrome dev tools allows us to take screenshot of a particular element. Even if it is a JavaScript method also i can get the data URI using driver.execute_script() command.
WebElement doesn't have save_screenshot. You can use screenshot_as_png (property) and save it
element = driver.find_element_by_css('#canvas-xyz')
scrrenshot = element.screenshot_as_png
with open('canvas.png', 'wb') as f:
f.write(scrrenshot)
Use WebElement.screenshot()method:
def screenshot(self, filename):
"""
Saves a screenshot of the current element to a PNG image file. Returns
False if there is any IOError, else returns True. Use full paths in
your filename.
:Args:
- filename: The full path you wish to save your screenshot to. This
should end with a `.png` extension.
:Usage:
element.screenshot('/Screenshots/foo.png')
"""
So, in your example, that would be:
driver.find_element_by_css('#canvas-xyz').screenshot('canvas.png')

open 'href' variable in a new tab

I'm using selenium and chrome webdriver with python.
I'm trying to store 'href' inside a variable ('link' for this example) and open it in a new tab.
i know how to open a dedicated website in a new tab using this way:
driver.execute_script("window.open('http://www.example.com', 'newtab')")
but using windows.open script accepts only direct text(as far as i know) and not variables.
Here is the code:
link = driver.find_element_by_class_name('asset-content').find_element_by_xpath(".//a[#class='mr-2']").get_attribute("href") #assigning 'href' into link variable. works great.
driver.execute_script("window.open(link, 'newtab')") #trying to open 'link' in a new tab
The error:
unknown error: link is not defined
Any other way i can open 'link' variable in a new tab?
You passing on a string to execute_script, so pass not a 'link' literally, but the value from the link (concatenate):
driver.execute_script("window.open('"+link+"','icoTab');")
Another way to open a tab is sending CTRL+T to the browser:
driver.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 't')
driver.get(link)
As mentioned, you can find more here 28431765/open-web-in-new-tab-selenium-python
Passing the parameter in scripts is not treating as url to make it url try This one. It works for me.
driver.execute_script("window.open('{},_blank');".format(link))
Please let me know if this work.

How to copy a text and store it into a variable using Selenium? Python

I want to copy a text from a website and store it into a variable so I can add it to a dictionary later. I wanted to know how do I accomplish it?
Here is my source code:
from selenium import webdriver
f = webdriver.Firefox()
f.get("http://hi.com")
g = f.find_element_by_tag_name('h1').getText()
print g
What type of text you need to copy from web page?
You need to locate webelement of text you need to capture and simply use getText() on it.
String text = driver.findElement(By.id("some id")).getText();

Categories

Resources