I have been trying to get an account balance text to print in python using selenium. I go to chrome, inspect the element, copy the xpath, put it in VS and it says cant find the xpath. I did the same thing for CSS selector, full xpath, tried to create my own xpath, tried by class name, etc. Ive tried every option it has. nothing is working. What do you do when you run into this situation? It is there on the website, its gotta be accessible in the background somewhere. The has to be a way to grab it. I have been able to get other text on the site to display properly.
This is just a stock simulator game, I have it open to the public if you want to log in and try your hand at getting the correct inspect element.
The xpath it generates:
/html/body/div[4]/div[3]/div[1]/div[2]/ul/li[1]/span
How I have it in VS:
driver.find_element_by_xpath('/html/body/div[4]/div[3]/div[1]/div[2]/ul/li[1]/span')
The error in VS:
Unable to locate element: {"method":"xpath","selector":"/html/body/div[4]/div[3]/div[1]/div[2]/ul/li[1]/span"}
To get the amount Net Worth Use WebDriverWait() and wait for visibility_of_element_located() and following xpath.
print(WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.XPATH,"//div[./span[text()='Net Worth']]/following::span[1]"))).text)
OR
print(WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.XPATH,"//ul[#class='list list--kv']/li[1]/span"))).text)
You need to import below libraries.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Related
i'm trying to create python automation scripts using selenium to make appointment times, however i'm having some trouble with clicking on certain elements. to confirm an appointment the steps are going to the webpage > selecting the date > selecting the time > hitting confirm
this is what i've got so far:
driver = webdriver.Chrome()
driver.get('<url.date>') -- this works
driver.find_element(By.ID,'corresponding time id').click() -- this works
driver.find_element(By.DATA-TEST-ID,'order_summary_page-button-book').click() -- this is where i'm struggling
i've also tried doing it by the button class, using the css selector and looking for 'Reserve Now' text, and using the xpath
when i do it by xpath, i get the error NoSuchElementException
i tried adding a webdriver wait function but that didn't seem to work either.
the appointment times are really hard to get, so i'd rather not but any wait times into the code so it can execute and book as soon as slots open
here are the elements:
https://i.stack.imgur.com/S62kR.png
any help is appreciated!
Selenium doesn't have option to identify the element by DATA-TEST-ID locator.
In this case You have to use either by xpath or css selector
driver.find_element(By.XPATH,'//button[#data-test-id="order_summary_page-button-book"]').click()
OR
driver.find_element(By.CSS_SELECTOR,'button[data-test-id="order_summary_page-button-book"]').click()
Ideally you should use explicit wait to click on the element.
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, '//button[#data-test-id="order_summary_page-button-book"]'))).click()
you need following imports.
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
I just started trying to learn to write python code today. I am trying to make a web scraper to do some stuff, but i am having trouble getting selenium to click on the specific boxes/places to get to the correct page. Because it uses Ember.js, it is a Single Page Application. This throws things off a little. I found some stuff on stackoverflow about it, but i wasnt able to get it to work. I keep getting the "NoSuchElementException" error. Here is my code
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
import time
PATH = r'C:\Users\dme03\Desktop\desktop\webdrivers\chromedriver_win32\chromedriver.exe'
driver = webdriver.Chrome(PATH)
driver.get('https://thedailyrecord.com/reader-rankings-interface/')
time.sleep(15)
mim = driver.find_element(By.XPATH, '//img[#src="//media.secondstreetapp.com/1973100?width=370&height=180&cropmode=Fill&anchor=Center"]/parent::div')
mim.click
For the XPATH, I had to select a child element and specify the parent to select the correct element. The key identifiers in the parent element kept changing, so i was unable to select just that. I believe the XPATH is correct because when I put that into the website, it showed up as the correct line.
I have also tried using the code below, as i have seen some people suggest. When using that, I get a timed out error.
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//img[#src="//media.secondstreetapp.com/1973100?width=370&height=180&cropmode=Fill&anchor=Center"]/parent::div')))
Any help would be appreciated
NEW CODE for iFrames
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, '//iframe[#src="https://embed-882706.secondstreetapp.com/embed/ae709978-d6d1-499e-9f5e-136cc36eab1b/"]')))
mim = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//img[#src="//media.secondstreetapp.com/1973100?width=370&height=180&cropmode=Fill&anchor=Center"]/parent::div')))
mim.click
I am having issues clicking the item. To be fair, i am not sure that you are supposed to click that element to change the webpage. That is probably an issue.
Edit: nvm im just dumb that totally worked lol :))
You are getting "NoSuchElementException" error because the desired element is inside the iframe .So you have to switch to iframe at first.See the below image screenshot
I was making a webscraper with selenium and bs4 to keep some shopping items' stock in track, there is a load more button I want to click which looks like this
button
and here is the HTML code for it, the website is https://www.nvidia.com/en-us/shop/geforce/?page=1&limit=9&locale=en-us
HTML code of the button
Whenever I try to find the element with bs4 it works but it never works with
driver.find_element_by_class_name("buy-link load-more-btn")
and I need it to click the button, can someone help me out
Am I was passing too many classes at once?
find_element_by_class_name accepts only one class. You are passing multiple classes in it.
Try implementing it with one class only
driver.find_element_by_class_name("load-more-btn").click()
Sort of a combination of the other two suggestions. If I understand Selenium correctly, it basically converts any find_element_* into a css selector, so having a space in the class name messes things up if you put both. This worked for me.
# Removes the Accept Cookies Banner
driver.find_element_by_css_selector("div.green-box-btn").click()
# Performs the click you want
driver.find_element_by_css_selector("input.buy-link").click()
driver.find_element_by_class_name("buy-link load-more-btn").click()
Please add click() to the end.
The LOAD MORE element is located at the bottom of the DOM Tree so to click on it you need to induce WebDriverWait for the element_to_be_clickable() which will automatically scroll the element within the Viewport and invoke click() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.buy-link.load-more-btn[value='LOAD MORE']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#class='buy-link load-more-btn' and #value='LOAD MORE']"))).click()
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
I am pretty new to Python and Selenium, and I have got my script to do what I want so far, but I have this current coding from the website:
<a onclick="realPostBack('ctl00$ctl00$mainContent$ContentPlaceHolder1$ucHub$ucSearchExplorer$dgContents$ctl00$ctl04$lnkContent', ''); return false;" id="ctl00_ctl00_mainContent_ContentPlaceHolder1_ucHub_ucSearchExplorer_dgContents_ctl00_ctl04_lnkContent" class="hub-content-item" actiontype="Secondary" href="javascript:__doPostBack('ctl00$ctl00$mainContent$ContentPlaceHolder1$ucHub$ucSearchExplorer$dgContents$ctl00$ctl04$lnkContent','')"><span>How to Project Wirelessly in Philly</span></a>
and I can't get it to click the link.
I've tried:
driver.find_element_by_text("How to Project Wirelessly in Philly")
and partial text
driver.find_element_by_id("ctl00_ctl00_mainContent_ContentPlaceHolder1_ucHub_ucSearchExplorer_dgContents_ctl00_ctl04_lnkContent")
I've tried by tag but all returning errors. Looking up on here, I've seen stuff with Xpath but I have no clue how to do that, but if someone here does, then a little help with that, or any other simple code that hopefully allows me to click that link. (I know i will have to do .click() to eventually click it, but i cant even find the element yet)
The desired element is a dynamic element, so invoke click() on it you have to induce WebDriverWait for the element to be clickable and you can use either of the following solutions:
Using CSS_SELECTOR:
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.hub-content-item[id*='SearchExplorer'][actiontype='Secondary']>span")))
Using XPATH:
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='hub-content-item' and contains(#id,'SearchExplorer')][#actiontype='Secondary']/span[text()='How to Project Wirelessly in Philly']")))
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
I am using selenium and python to automate web tasks. I have tried using multiple different functions to try to click on the button I need:
Reports
.find_element_by_link_text("Reports").click()
.find_element_by_id
.find_element_by_name
.find_element_by_class_name
.find_element_by_css_selector
Can't seem to make this work, any suggestions would be appreciated.
Using "find_elements_by_xpath". Right click and copy the XPATH from the browser.
Using explicit waits could potentially solve the problem. For explicit waits, you can either use the ID or XPATH. I prefer using XPATH. For getting the XPATH, right click on the element, click Inspect, right click on Reports and choose Copy, and then Copy XPATH. Now that we have the XPATH, do the following:
button_xpath = "the xpath of your element"
button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, button_xpath))).click()
The above code will wait up to 10 seconds until the button element is found. If the element is not found, a TimeoutException will be given.
The following imports are necessary:
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