How can I take screenshot of only relevant content of any webpage using Selenium and Python?
I want to take the screenshot of the marked content (specifications) in this photo instead of whole page
Example webpage link
Currently I'm taking screenshot of the whole page. Also I want avoid referencing any class or id while taking the screenshot. Please let me know if I can achieve this (if yes, HOW?) or have to change my requirements. If there is any workaround such as cropping the relevant content, please do share too. Thanks.
Chrome appears to be a bit temperamental when screenshotting - only takes what's visible on screen, so I would advise Firefox/geckodriver for these kind of jobs. The following will take a full screenshot of that element:
from selenium import webdriver
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.options import Options as Firefox_Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support import expected_conditions as EC
import time as t
firefox_options = Firefox_Options()
firefox_options.add_argument("--width=1280")
firefox_options.add_argument("--height=720")
# firefox_options.headless = True
driverService = Service('chromedriver/geckodriver')
browser = webdriver.Firefox(service=driverService, options=firefox_options)
actions = ActionChains(browser)
url = 'https://www.startech.com.bd/benq-gw2480-fhd-monitor'
browser.get(url)
elem = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//section[#id='specification']")))
elem.screenshot('fullspec.png')
print('screenshotted specs')
Related
I am new to python and selenium. Trying to automate download of files from the dropdown menu on the below link for the first time. The code gets stuck after page loads i.e the drop down does not work and gives me an error. Sorry if my code looks clunky. Any help is appreciated.
Thanks
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
import time
s=Service("C:\\python\\chromedriver.exe")
driver = webdriver.Chrome(service=s)
driver.maximize_window()
driver.get('http://www.amfiindia.com/research-information/aum-data/classified-average-aum')
#monthname=driver.find_element(By.XPATH,"//*[#id='AaumDate']")
monthdd=Select(monthname)
monthdd.select_by_value('01-Jan-22')
time.sleep(3)
typename=driver.find_element(By.XPATH,"//*[#id='AumType']")
typedd=Select(typename)
typedd.select_by_visible_text('Scheme category wise')
time.sleep(3)
mfname=driver.find_element(By.XPATH,"//*[#id='AumMFName']")
mfdd=Select(mfname)
mfdd.select_by_visible_text('All')
time.sleep(3)
driver.find_element("class name","sprite-inter go-btn").click()
wait=WebDriverWait(driver,20)
driver.get('http://www.amfiindia.com/research-information/aum-data/classified-average-aum')
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"#divAumPeriod > span > a > span.ui-button-text"))).click()
wait.until(EC.element_to_be_clickable((By.XPATH,"//*[#class='ui-menu-item']/a[.='January - 2022']"))).click()
Most of these tags open like so and have another list with them that's not using the select tag.
Imports:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
I'm trying to click a button with its class but it throws an ElementNotInteractableException.
Here is the website HTML code
Here is the code I'm using
driver = webdriver.Chrome('chromedriver.exe', chrome_options=options)
driver.get('https://physionet.org/lightwave/?db=noneeg/1.0.0')
def get_spo2hr(subject):
driver.find_element_by_xpath("//select[#name='record']/option[text()='"+subject+"']").click()
driver.find_element_by_id('ui-id-3').click()
driver.find_element_by_id('viewann').click()
driver.find_element_by_id('viewsig').click()
driver.find_element_by_id('lwform').click()
driver.find_element_by_css_selector(".fwd").click()
driver.save_screenshot('screenie.png')
get_spo2hr('Subject10_SpO2HR')
One thing is (as said in other answers) the unstable css selector prefer xpath
But the main thing is that the div is overlapping the a item at the dom rendering
Just wait one second to wait until the dom loads:
import time
time.sleep(1)
Example code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
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
driver = webdriver.Chrome()
driver.get('https://physionet.org/lightwave/?db=noneeg/1.0.0')
def get_spo2hr(subject):
driver.find_element_by_xpath("//select[#name='record']/option[text()='"+subject+"']").click()
import time
time.sleep(1)
driver.find_element_by_id('ui-id-3').click()
driver.find_element_by_id('viewann').click()
driver.find_element_by_id('viewsig').click()
driver.find_element_by_id('lwform').click()
driver.find_element_by_xpath('/html/body/div[1]/main/div/div/div/form/div[3]/table/tbody/tr/td[2]/div/button[3]').click()
driver.save_screenshot('screenie.png')
get_spo2hr('Subject10_SpO2HR')
I always prefer getting elements using their xpath, of course, in suitable situations. With that being said, I modified your code to find the forward button using its xpath and it works.
Here is the modified code:
driver = webdriver.Chrome('chromedriver.exe', chrome_options=options)
driver.get('https://physionet.org/lightwave/?db=noneeg/1.0.0')
def get_spo2hr(subject):
driver.find_element_by_xpath("//select[#name='record']/option[text()='" + subject + "']").click()
driver.find_element_by_id('ui-id-3').click()
driver.find_element_by_id('viewann').click()
driver.find_element_by_id('viewsig').click()
driver.find_element_by_id('lwform').click()
driver.find_element_by_xpath('/html/body/div[1]/main/div/div/div/form/div[3]/table/tbody/tr/td[2]/div/button[3]').click()
driver.save_screenshot('screenie.png')
get_spo2hr('Subject10_SpO2HR')
I am having problems with my selenium script.
I want to webscrape a website that is running on javascript.
I have been gathering a lot of information on the internet but i can't find the solution.
picture of the html code
picture of my code
In this post i have also submitted a printscreen of the HTML code.
Basically: i want click on the accept button when i load the website but i can't figure how to do this.
searching on different websites for the solution.
Can you guys help me with my script, i have been trying and testing a lot but i can't figure it out.
thank you.
code:
from config import keys
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import time
def order(k):
chrome_path =
r"C:\Users\ltewo\PycharmProjects\livebetting\chromedriver.exe"
driver = webdriver.Chrome(chrome_path)
driver.get(keys['url'])
xpath_button_accept = "//div[#class='cookieButtons']//a[#class='button
accept']"
button_accept = driver.find_element_by_xpath(xpath_button_accept)
xpath_button_accept.click()
if __name__ == '__main__':
order(keys)
You should wait for that element to load on the page, so use:
WebDriverWait(browser, delay).until(EC.presence_of_element_located((By.XPATH, 'XpathOfMyElement')))
I noticed that the cookie panel was inside an iframe and was not directly accessible, I have updated the below, please check it's working now.
Updated your code:
from config import keys
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import time
def order(keys):
chrome_path = r"C:\Users\ltewo\PycharmProjects\livebetting\chromedriver.exe"
driver = webdriver.Chrome(chrome_path)
driver.get(keys['url'])
driver.switch_to.frame('r42CookieBar')
button_accept = driver.find_element_by_class_name('accept')
button_accept.click()
if __name__ == '__main__':
order(keys)
Try this:
#update
driver.implicity_wait(10)
xpath_button_accept = "//div[#class='cookieButtons']//a[#class='button accept']"
button_accept = your_browser.find_element_by_xpath(xpath_button_accept)
button_accept.click()
i'm trying to login this site with selenium -python
but i can't .i read another question but i didn't get it.
how can i do it?
this my error:
enter image description here
this is my code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Chrome()
driver.get("https://en.gamefa.com/")
driver.find_element_by_name("loginform").submit()
driver.find_element_by_class_name("ModalBoxBody")#.submit()
time.sleep(10)
elem1 = driver.find_element_by_name("log")
elem1.send_keys("mehrdad78")
elem2 = driver.find_element_by_name("pwd")
elem2.send_keys("mehrdad78").submit()
First, Your screenshot is not giving us enough data about the error. Post the error stack trace which directs to your code.
Second, all the items have id. It would be better to use id to find elements.
Third, Click does work. Submit doesn't work on buttons. using submit on an element outside a form should throw an exception.
Fourth, Use explicit wait instead of sleep.
Try this
from selenium import webdriver
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.by import By
driver.get("https://en.gamefa.com/")
driver.find_element_by_id("login-form").click()
elem1 = WebDriverWait(driver,5).until(EC.visibility_of_element_located((By.ID, "user_login3")).send_keys("mehrdad78")
elem2 = driver.find_element_by_id("user_pass3").send_keys("mehrdad78")
elem3 = driver.find_element_by_id("wp-submit3").submit()
I want to click first on the link of the table with text A218012216.
It seems that table/links code are hidden inside JS.
I tried several ways, but without success.
This is my code so far:
import time
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
url0 ="https://ccrecordse.tarrantcounty.com/AssumedNames/SearchEntry.aspx"
driver = webdriver.Chrome(executable_path="D:\Python\chromedriver.exe")
driver.get(url0)
time.sleep(3)
#fill the form # select by visible text
selectStart = driver.find_element_by_id('x:11265151.0:mkr:3')
selectStart.send_keys('09/05/2019')
selectEnd = driver.find_element_by_id('x:1246303050.0:mkr:3')
selectEnd.send_keys('09/05/2019')
#submit the form
driver.find_element_by_id("cphNoMargin_SearchButtons2_btnSearch__5").click()
time.sleep(3)
driver.find_element_by_link_text('A218012216').click()
How can I get that information?
The problem is that you are trying to use find_element_by_link_text on an element that is not an <a> tag link_text is only for <a> tags...
In my solution, you will see I am using WebDriverWait this is best practice in selenium...
Also, I used the XPath locator with text()=the_text note that the the_text will need to change as the date changes (you are searching for the future date 09/05/2019 so it shows the current date therefor the text will change...)
The Solution:
import time
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
url0 ="https://ccrecordse.tarrantcounty.com/AssumedNames/SearchEntry.aspx"
driver = webdriver.Chrome(executable_path=r"D:\Python\chromedriver.exe")
driver.get(url0)
time.sleep(3)
#fill the form # select by visible text
selectStart = driver.find_element_by_id('x:11265151.0:mkr:3')
selectStart.send_keys('02/19/2019')
selectEnd = driver.find_element_by_id('x:1246303050.0:mkr:3')
selectEnd.send_keys('02/19/2019')
#submit the form
driver.find_element_by_id("cphNoMargin_SearchButtons2_btnSearch__5").click()
the_text = "A219002410"
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[text()='"+the_text +"']"))).click()
Hope this helps you!