Python Selenium getting trading view URL of the snapshot - python

I am trying to get the snapshot URL from the snapshot window that opens when ALT + S is pressed. I can save the image. but instead i want to get the URL that is in the snapshot box and save it in a variable
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC1
from selenium.webdriver.common.by import By
driver = webdriver.Chrome('C:\\Windows\\chromedriver.exe') # change as per your location
driver.get ("https://www.tradingview.com/chart/1pPtW0th/")
driver.maximize_window()
ActionChains(driver).key_down(Keys.ALT).send_keys('s').perform()
wait_time = 25 # a very long wait time
#want to get snapshot URL here, instead of saving it.
element = WebDriverWait(driver, wait_time).until(EC1.element_to_be_clickable((By.LINK_TEXT, 'Save image')))[enter image description here][1]
time.sleep(3)
driver.close()

You could try targeting the following <div> element and extract the attribute "data-clipboard-text":
<div data-clipboard-text="https://www.tradingview.com/x/ODEmfXoU/" class="copyBtn-1oB8tEqo- shadow-2JTdO0Fb-">
<button class="button-2O-nMUcz- active-2UxWxOgk- withPadding-_5CJoO5q- ghost-3yO24wIn- primary-1rSzOFdX-" tabindex="0" title="" target="" href="">
<span class="hiddenText-3qcN5Wif-">Copy</span>
<span class="text-2KOWx3rB-">Copy</span>
</button>
</div>

Related

Selenium no such element exception

Im trying to get this link
<ul class="row">
<li class="col-md-3 col-xs-12 col-sm-6 episodeLink37027" data-lang-key="1" data-link-id="37027" data-link-target="/redirect/37027" data-external-embed="false">
<div class="generateInlinePlayer">
<a class="watchEpisode" itemprop="url" href="/redirect/37027" target="_blank">
<i class="icon VOE" title="Hoster VOE"></i>
<h4>VOE</h4>
<div class="hosterSiteVideoButton">Video öffnen</div>
...
I already tried:
button = driver.find_element_by_css_selector("watchEpisode")
and
button = driver.find_element_by_class_name("a.watchEpisode")
I always get an no such element Exception. Are there other ways to get this element?
Try this
button = driver.find_element_by_css_selector("a.watchEpisode")
or
button = driver.find_element_by_class_name("watchEpisode")
Don't forget to add a delay / wait before accessing this element
UPD
I see no iframes there, but the locator can be improved.
Try this code:
from selenium import webdriver
from selenium.webdriver.chrome.webdriver import WebDriver
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
import time
CHROMEDRIVER = r"C:\Users\Nico\Desktop\automate_download\chromedriver.exe"
driver = webdriver.Chrome(executable_path=CHROMEDRIVER)
driver.maximize_window()
wait = WebDriverWait(driver, 20)
actions = ActionChains(driver)
driver.get("https://anicloud.io/anime/stream/attack-on-titan")
wait.until(EC.visibility_of_element_located((By.XPATH, '//*[#title="Staffel 1 Episode 1"]'))).click()
button = wait.until(EC.presence_of_element_located((By.XPATH, "//li[not(contains(#style,'none'))]//i[#title='Hoster VOE']/..//div[#class='hosterSiteVideoButton']")))
actions.move_to_element(button).perform()
time.sleep(0.5)
#now you can click this element
button.click()
Try this one, see if it works.
button = driver.find_element_by_xpath("//*[#class='watchEpisode']")
probably you can try with this css selector :
div.generateInlinePlayer>a.watchEpisode
or this xpath:
//div[#class='generateInlinePlayer']//child::a[#class='watchEpisode']
PS : Please check in the dev tools (Google chrome) if we have unique entry in HTML DOM or not.
Steps to check:
Press F12 in Chrome -> go to element section -> do a CTRL + F -> then paste the xpath and see, if your desired element is getting highlighted with 1/1 matching node.
to get the href you can try the below code :
Code trial 1 :
time.sleep(5)
val = driver.find_element_by_xpath("//div[#class='generateInlinePlayer']//child::a[#class='watchEpisode']").get_attribute('href')
print(val)
Code trial 2 :
val = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#class='generateInlinePlayer']//child::a[#class='watchEpisode']"))).get_attribute('href')
print(val)
Code trial 3 :
time.sleep(5)
button = driver.find_element_by_xpath("//div[#class='generateInlinePlayer']//child::a[#class='watchEpisode'])
val = driver.execute_script("return arguments[0].value", button)
print(val)
Code trial 4 :
time.sleep(5)
button = driver.find_element_by_xpath("//div[#class='generateInlinePlayer']//child::a[#class='watchEpisode']")
ActionChains(driver).move_to_element(button).perform()
print(button.get_attribute('href'))
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains

Finding an element using find_element_by_xpath

I'm trying to find an element by xpath, but it does not return anything.
It seems all find_elements methods work except xpath.
I'm trying to find the text Ponderosa Campground which is clickable.
def searchCondition():
elem = driver.find_elements_by_xpath("//div[#aria-label='PONDEROSA CAMPGROUND']")
elem.click()
here is the URL where I'm trying to find the element.
https://www.recreation.gov/search?q=Ponderosa%20Campground
<div data-component="FocusManager" tabindex="-1" style="outline: none;">
<div class="flex-grid search-outer-wrap" data-component="FlexRow">
<div class="flex-col-12" data-component="FlexCol">
<div class="rec-flex-card-wrap " id="rec-card-233118_campground">
<a data-card="true" class="rec-flex-card-image-wrap" href="/camping/campgrounds/233118" target="_blank" rel="noopener noreferrer" alt="PONDEROSA CAMPGROUND">
<div data-component="FauxImage" class="sarsa-faux-image rec-flex-card-image-wrap-faux-image" role="img" aria-label="PONDEROSA CAMPGROUND" style="min-height: 155px; background-image: url("https://cdn.recreation.gov/public/images/76799.jpg");"></div></a>
<div class="rec-flex-card-content-wrap"><a href="/camping/campgrounds/233118" target="_blank" rel="noopener noreferrer" aria-label="PONDEROSA CAMPGROUND - 2.6 stars / $25 / night">
Here is the full script.
import time
from time import sleep
from datetime import datetime
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.chrome.options import Options
import pause
import pyperclip
# Definitions
ID = ""
PW = ""
SRCH = "Ponderosa Campground"
URL = "https://www.recreation.gov/search?q=Ponderosa%20Campground"
now = datetime.now()
options = Options()
options.headless = False
# executable_path for webdriver
driver = webdriver.Chrome(
executable_path='C:/chromedriver.exe',
options=options)
wait = WebDriverWait(driver, 50)
driver.get(URL)
def searchClick():
CampBtn = wait.until(EC.element_to_be_clickable((By.XPATH,"//a[#alt='PONDEROSA CAMPGROUND']"))).click()
elem = driver.find_element_by_xpath("//a[#alt='PONDEROSA CAMPGROUND']")
if elem.isDisplayed():
print(elem)
elem.click()
else:
print("no availability")
searchCondition()
time.sleep(20)
You have to point your xpath to the interactive element for example a, input, button
And have to induce some explicit wait with expected condition to check that the element is all set to perform click operation. For example -
WebDriverWait(web, 20).until(EC.element_to_be_clickable((By.XPATH,"//a[#alt='PONDEROSA CAMPGROUND']")))
elem = web.find_element_by_xpath("//a[#alt='PONDEROSA CAMPGROUND']")
elem.click()
After clicking on the image it opens up new tab as below. To see this operation add some wait for sometime after click operation -

How to use derive an X-Path from a specific HTML attribute to use with Selenium

Background:
I have the following HTML code that I am trying to get a XPATH to use with Selenium
<div class="btn-group">
<a type="button" class="btn btn-primary btn-user" onclick="AiD('182030801')" href="/download.pdf?id=182030801&h=917901e6659ad5eb53970aecf687b53e&u=cache&ext=pdf" target="_blank" style="border-top-left-radius: 3px;border-bottom-left-radius: 3px;">
<i class="fas fa-cloud-download-alt" aria-hidden="true" style="margin-right: 9px;margin-left: 2px;font-size: 25px;vertical-align: middle;color: #119802;"></i>Download ( PDF )
</a>
[...]
</div>
Code:
What have I tried to do with Python's Selenium is the following, however I cannot quiet get it to work without Python throwing an error:
browser.find_element(By.XPATH, "//div[#class='btn-group']/a").click()
browser.find_element(By.XPATH, "//div[#class='btn tn-primary btn-user']").click()
Error:
When I run the above snippet of code, the following error is produced and script crashes thereafter:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//div[#class='btn tn-primary btn-user']"}
Problem:
The error states "no such element", however the element clearly exists "class="btn btn-primary btn-user""
Question:
How can I use Seleniums XPATH to "see" class="btn btn-primary btn-user" and click it to download a PDF?
Link:
PDFDrive
USe Xpath //div/a[#class='btn btn-primary btn-user']
Make sure the intended element is not under an iFrame. If it is then first you need to switch into that iFrame and then have to perform the action
Make sure you are using proper synchrnization and your element is ready. Introduce explicit wait as below:
driver.get('https://www.pdfdrive.com/querying-xml-xquery-xpath-and-sqlxml-in-context-d38665640.html')
WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[#class='btn-group']/a"))).click()
time.sleep(10) // it doesn't recommanded to put hardcoded wait but for debugging purpose you can check
Import below packages for this:
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
You are using "div" tag but I can see respective class = btn btn-primary btn-user is inside "a" tag
try below code :
browser.find_element(By.XPATH, "//a[#class='btn tn-primary btn-user']").click()
Or you can go with another locator as well.
CSS : a.btn.btn-primarybtn-user
So you need to wait for the element to be clickable. Triggered the download.
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
options = Options()
driver = webdriver.Chrome(ChromeDriverManager().install(),options=options)
url = 'https://www.pdfdrive.com/querying-xml-xquery-xpath-and-sqlxml-in-context-d38665640.html'
driver.get(url)
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,'a.btn.btn-primary.btn-user'))).click()

How can make (style =“display: none;”) element visible in python selenium

<div class="clearfix entry_but_bar_out clearfix" style="display: none;">
<div class="entry_but_bar_in clearfix">‹ yanıtlaeditsilmesajgammazla#208281235<i class="ssicon-facebook"></i>
<i class="ssicon-twitter"></i>
</div><!-- entry_but_bar_in clearfix -->
</div>
i'm trying to make elements visible and click on it . But it doesn't work ,
clicking = browser.find_element_by_xpath('//*[#id="middle-block"]/ol/li/div/div[3]/div[2]/span')
browser.execute_script("removeAttribute('display:none');",clicking)
clicking.click()
Please try the below code and let me know if it works for you.
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver import ActionChains
import time
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 20)
action = ActionChains(driver)
# Put your URL
driver.get('Your URL')
time.sleep(5)
Target_Div = driver.find_element_by_xpath('//div[contains(#class,"clearfix entry_but_bar_out clearfix")]')
driver.execute_script("arguments[0].setAttribute('style','display:\"true\"')", Target_Div)
Use the above code and make correction in your code.

Python Selenium select div class with blanks

I've got a problem while trying to select a div tag class that has some white spaces in it.
This is the structure of page:
<div class="sadasd-dashboardtab even asdasd-syndicating_from_my_file" id="124121_1540012412412414">
<div id="124121_154006585856856858">
<span class="label">Syndicating From My File</span>
<button class="column-dropdown" title="Click for more tab options"></button>
</div>
</div>
This are my tries of code for this part:
#syndicating_button = driver.find_element_by_xpath("//span[text()='Syndicating From My File']")
#syndicating_button = driver.find_element_by_xpath("//div[#class='yui3-dashboardtab even s-tab-syndicating_from_my_site']")
syndicating_button = driver.find_element_by_css_selector("div.yui3-dashboardtab.even.s-tab-syndicating_from_my_site")
syndicating_button.click()
Your issue has nothing to do with "blanks"/"whitespaces" as your selectors should work well... if element is present in DOM. Try to wait until element appears in DOM and becomes clickable:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC
wait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Syndicating From My Site']"))).click()

Categories