I'm trying to extract past one year prices.
Unable to get the XPath for the drop down.
Here's my recent code:
element1 = driver.find_element_by_xpath("""//button[#title="1 year"]""")
element2 = driver.find_element_by_xpath("""//*[#id="chartmenu"]/li/a""")
hoverover = ActionChains(driver).move_to_element(element1).move_to_element(
element2).click().perform()
Where am I wrong here? Please help!
To click() on the dropdown element with text as Download Price Index CSV you need you have to induce WebDriverWait for the element_to_be_clickable() and you can use the following Locator Strategies:
Code Block:
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
from selenium.webdriver.common.action_chains import ActionChains
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("start-maximized")
driver = webdriver.Chrome(options=chrome_options, executable_path=r'C:\WebDrivers\chromedriver.exe')
driver.get("https://www.deribit.com/prinx_chart")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.btn-group.btn-group-sm button[title='1 year']"))).click()
download_element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.highcharts-container ")))
ActionChains(driver).move_to_element(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "a.dropdown-toggle")))).perform()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "ul#chartmenu a[href*='PrChart'] span[data-i18n*='price_index_csv_download']"))).click()
Browser Snapshot:
Wait and check success login, than try to download. Here improved code:
driver.find_element_by_xpath("//input[#type = 'email']").send_keys(************)
driver.find_element_by_xpath("//input[#type = 'password']").send_keys(*********)
driver.find_element_by_name("go").click()
# wait until success login, by waiting some element on home page
driver.get('https://www.deribit.com/prinx_chart')
driver.find_element_by_xpath("//button[#title='1 year']").click()
driver.find_element_by_css_selector("span[data-i18n='app.price_index_csv_download']").click()
Related
I'm trying to get the attributes of a tag using selenium webdriver and using the xpath as a locator. I gave the xpath to the driver and it returned NoSuchElementException, but when I enter the xpath in the "Inspect element" window, it showed that particular tag, which means the locator does exist. So what's wrong with selenium? Its still the same even if I give the full xpath
from selenium import webdriver
driver = webdriver.Chrome('D:\\chromedriver.exe')
driver.get('https://cq-portal.webomates.com/#/login')
element=driver.find_element_by_xpath("//button[#type='button']")
print(element.get_attribute('class'))
driver.quit()
selenium version = 3.141.0
You need to just give wait to load the page. Your code is perfectly fine. Either give hardcode wait like sleep or presence of element. Both will work.
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 import webdriver
driver = webdriver.Chrome(PATH)
driver.maximize_window()
wait = WebDriverWait(driver, 20)
driver.get('https://cq-portal.webomates.com/#/login')
wait.until(EC.presence_of_element_located((By.XPATH, "//button[#type='button']")))
element = driver.find_element(By.XPATH, "//button[#type='button']")
print(element.get_attribute('class'))
driver.quit()
Output:
btn btn-md btn-primary btn-block
Loops like you are missing a delay.
Please try this:
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 import webdriver
driver = webdriver.Chrome('D:\\chromedriver.exe')
wait = WebDriverWait(driver, 20)
driver.get('https://cq-portal.webomates.com/#/login')
wait.until(EC.visibility_of_element_located((By.XPATH, "//button[#type='button']")))
element=driver.find_element_by_xpath("//button[#type='button']")
print(element.get_attribute('class'))
driver.quit()
I'm trying to scrape a website with show more button; and I'm not able to click on it.
The website is: https://www.wtatennis.com/rankings/singles
And my code is:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver import ActionChains
from tqdm import tqdm
import time
options = Options()
options.add_argument("--headless")
browser = webdriver.Chrome(ChromeDriverManager().install(),options=options)
browser.get('https://www.wtatennis.com/rankings/singles')
action = ActionChains(browser)
showmore = browser.find_elements_by_xpath(".//button[contains(#class, 'btn widget-footer__more-button rankings__show-more js-show-more-button')]")
action.move_to_element(showmore).perform()
showmore.click()
time.sleep(5)
Has anyone any idea? Thanks!
Don't use './/' in your locator when you are starting the search from root, as there is no current element your locator won't find any element. Also you can use any attribute to find elements uniquely. see below code:
browser = webdriver.Chrome(options=options)
browser.get('https://www.wtatennis.com/rankings/singles')
WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.XPATH,
'//*[#data-text="Accept Cookies"]'))).click()
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH,
'//*[#data-text = "Show More"]'))).click()
use webdriver wait and data attributes
tu use wait import:
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
To wait till all elements are loaded you have to make sure last element is not changing , if its changing keep scrolling .
browser.get('https://www.wtatennis.com/rankings/singles')
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH,
'//*[#data-text="Accept Cookies"]'))).click()
value = "start"
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH,
'//*[#data-text = "Show More"]'))).click()
while(browser.find_element_by_xpath("(//tr[#class='rankings__row'])[last()]").text != value):
elem = browser.find_element_by_xpath(
'(//*[contains(text(),"Loading")])[2]')
value = browser.find_element_by_xpath(
"(//tr[#class='rankings__row'])[last()]").text
browser.execute_script("arguments[0].scrollIntoView()", elem)
WebDriverWait(browser, 10).until(EC.presence_of_all_elements_located((By.XPATH,
"//tr[#class='rankings__row']")))
try:
WebDriverWait(browser, 10).until_not(EC.text_to_be_present_in_element((By.XPATH,
"(//tr[#class='rankings__row'])[last()]"), value))
except:
None
I want to verify either of the page title "Apple" or "Mango" using EC.title_contains() method.
I have tried below but does't work for both the pages.
WebDriverWait(driver, 10).until(EC.title_contains("Apple") or EC.title_contains("Mango"))
WebDriverWait(driver, 10).until(EC.title_contains("Apple" or "Mango"))
Hi Check if below lines can help you to know the page by page title, change the driver path..
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
opt = webdriver.ChromeOptions()
opt.add_argument("--start-maximized")
driver = webdriver.Chrome(executable_path="C:\\chrome driver\\chromedriver.exe", options=opt)
driver.get("http://rera.rajasthan.gov.in/ProjectSearch")
WebDriverWait(driver,15).until(lambda driver: 'Mango' in driver.title or 'RER' in driver.title)
search_btn = driver.find_element_by_xpath('//*[#id="btn_SearchProjectSubmit"]')
# invoke the click() action
search_btn.click()
For Element below lines need to import By
from selenium.webdriver.common.by import By
WebDriverWait(driver,15).until(lambda driver: driver.find_element(By.XPATH,'xpath') or driver.find_element(By.XPATH,'xpath2nd'))
I am trying to click and download "Real Sector" on the following link:
http://www.sbp.org.pk/reports/quarterly/fy19/Second/qtr-index-eng.htm
Here is what I have tried:
driver.get('http://www.sbp.org.pk/reports/quarterly/fy19/Second/qtr-index-eng.htm')
try:
driver.find_element_by_css_selector("a[href= 'Chap-2.pdf']").click()
except NoSuchElementException:
pass
But it gives following error:
ElementNotInteractableException: Message: Element could not be scrolled into view
How can I resolve this?
You need to apply Explicit Wait and wait till the element is present on the page and then you can click on it.
You can also scroll to the element first and then click on it.
You can do it like:
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
driver.get('http://www.sbp.org.pk/reports/quarterly/fy19/Second/qtr-index-eng.htm')
try:
element = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "//a[#href='Chap-2.pdf']")))
driver.execute_script("arguments[0].scrollIntoView();", element)
element.click()
except NoSuchElementException:
pass
OR
You can directly click on the element using java script click like:
driver.get('http://www.sbp.org.pk/reports/quarterly/fy19/Second/qtr-index-eng.htm')
try:
element = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "//a[#href='Chap-2.pdf']")))
driver.execute_script("arguments[0].click();", element)
except NoSuchElementException:
pass
Try to wait use element_to_be_clickable
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[href= 'Chap-2.pdf']")))
element.click()
To click on "Real Sector" link Induce WebDriverWait() and element_to_be_clickable() and following xpath option.
driver = webdriver.Chrome()
driver.get('http://www.sbp.org.pk/reports/quarterly/fy19/Second/qtr-index-eng.htm')
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(.,'Real Sector')]"))).click()
You need to import following libraries.
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
Browser snapshot after clicking.
I am trying to download history data and click on link to historical data. However even though the Xcode is correct I get this error:
NoSuchElementException: no such element: Unable to locate element.
Code trials:
from selenium.webdriver.support.ui import WebDriverWait
from selenium import webdriver
driver = webdriver.Chrome(executable_path='/Users/Documents/Coding/chromedriver')
url = "https://www.vanguardinvestor.co.uk/investments/vanguard-lifestrategy-100-equity-fund-accumulation-shares/price-performance?intcmpgn=blendedlifestrategy_lifestrategy100equityfund_fund_link"
driver.get(url)
wait = WebDriverWait(driver, 10)
elem = driver.find_element_by_xpath("//*[#id='prices-and-performance-tab']/div/div[4]/div[3]/div[1]/div[1]/div[3]/div/div/div[2]/div/table/tfoot/tr/td/a")
webdriver.ActionChains(driver).move_to_element(elem).click(elem).perform()
To click on the element wait for the page to load and element to be clickable and then click.Try entire snippet.
from selenium.webdriver.support.ui import WebDriverWait
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver = webdriver.Chrome(executable_path='/Users/Documents/Coding/chromedriver')
url = "https://www.vanguardinvestor.co.uk/investments/vanguard-lifestrategy-100-equity-fund-accumulation-shares/price-performance?intcmpgn=blendedlifestrategy_lifestrategy100equityfund_fund_link"
driver.get(url)
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()[contains(.,'Price & Performance')]]")))
element.click
To click on the element with text as Search for more historical prices you need to induce WebDriverWait for the element to be clickable and you can use the following solution:
Code Block:
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
options = webdriver.ChromeOptions()
options.add_argument("--start-maximized")
options.add_argument("--disable-extensions")
driver = webdriver.Chrome(chrome_options=options, executable_path = r'C:\Utility\BrowserDrivers\chromedriver.exe' )
driver.get("https://www.vanguardinvestor.co.uk/investments/vanguard-lifestrategy-100-equity-fund-accumulation-shares/price-performance?intcmpgn=blendedlifestrategy_lifestrategy100equityfund_fund_link")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#id='bannerButton']"))).click()
more_historical_prices = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.LINK_TEXT, "Search for more historical prices")))
driver.execute_script("arguments[0].scrollIntoView(true);", more_historical_prices)
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Search for more historical prices")))
Browser Snapshot: