There is a button on the page that when I clicked copies a link. check the below image
I'm trying to store that link in a variable with python selenium this way:
surl=ii.find_element(By.XPATH, f'/html/body/div[1]/div[2]/div[5]/div[1]/div/div/div/section/div/div[2]/div/div[2]/div[1]/div/div/div/div[3]/div[2]/table/tbody/tr[str({row+1})]/td[6]/div/div/span/div/button')
furl = surl.send_keys(Keys.CONTROL + "c")
print(furl)
But I get the result as None. What is the issue here, Could someone help me with this?
Thank you.
By clicking that button the link is copied to the clipboard.
There are several ways to get the text content from clipboard with Python. For example try this:
from tkinter import Tk
surl=ii.find_element(By.XPATH, f'/html/body/div[1]/div[2]/div[5]/div[1]/div/div/div/section/div/div[2]/div/div[2]/div[1]/div/div/div/div[3]/div[2]/table/tbody/tr[str({row+1})]/td[6]/div/div/span/div/button')
surl.click()
link = Tk().clipboard_get()
print(link)
Related
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'")
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 can to store this link my link in the clipboard to be able to past it in HTML mode (and not source code) in an HTML editor?
Pasting it in an editor should only show the text my link with a clickable link.
Using Tkinter or pywin32 (or others), how to tell the clipboard that it contain html content (and not just raw text)?
Based on the link suggested by #chrki.
You can do this:
Install HtmlClipboard : copy the script, save it as HtmlClipboard.py in C:\Python##\Lib\site-packages\
Save the script below as link_as_html.py(I used some of your code in your question):
Create a shorcut for the script in step to (right click on the file link_as_html.py, and select create a shorcut)
Right click on the shorcut, select Properties, and and add a keyboard shorcut in Shorcut key.
That's it. When you have an link in our clipboard, you can just press your keyboard shorcut and you can paste your image directly in the html mode of you editor.
link_as_html.py (Python34). I assume you have your url http://www.web.com in the clipboard:
from tkinter import Tk
root = Tk()
root.withdraw()
url = root.clipboard_get()
# send my link to an "HTML format clipboard"
import HtmlClipboard
HtmlClipboard.PutHtml("<a href=\"http://"+url+" \" target=\"_blank\"/>my link</a>")
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();
I am trying to write a python script that will generate the rank-list of my batch. For this I simply need to change the roll-number parameter of the link using inspect element feature in web-browser. The link(relative) looks something like:
/academic/utility/AcademicRecord.jsp?loginCode=000&loginnumber=000&loginName=name&Home=ascwebsite
I just need to change the loginCode to get the grade of my batch-mates. I am trying to use python to iterate through all the roll-numbers and generate a rank-list. I used mechanize library to open the site using python. The relevant portion of code:
br = mechanize.Browser()
br.set_handle_robots(False)
response = br.open('link_to_the_page')
I then do the necessary authentication and navigate to the appropriate page where the link to view the grades reside.
Then I find the relevant link like this:
for link in br.links(url_regex='/academic/utility/AcademicRecord.jsp?'):
Now inside this I change the url and attributes of the link appropriately.
And then I open the link using:
response=br.follow_link(link)
print response.read()
But it does not work. It opens the same link i.e. with the initial roll number. In fact I tried changing the url of the link to something very different like http://www.google.com.
link.url='http://www.google.com'
link.base_url='http://www.google.com'
It still opens the same page and not google's page.
Any help would be highly appreciated.
According to the source code, follow_link() and click_link() use link's absolute_url property that is set during the link initialization. And, you are setting only url and base_url properties.
The solution would be to change the absolute_url of a link in the loop:
BASE_URL = 'link_to_the_page'
for link in br.links(url_regex='/academic/utility/AcademicRecord.jsp?'):
modified_link = ...
link.absolute_url = mechanize.urljoin(BASE_URL, modified_link)
br.follow_link(link)
Hope that helps.