On this page 'https://www.nj.gov/health/cd/topics/covid2019_dashboard.shtml', I try to get the number of New Jersey Positives (upper right of the dashboard).
positiveCount = driver.find_elements_by_xpath("//text[#style='stroke-width: 2; font-size: 160px; line-height: normal;']")
print len(positiveCount)
it always show 0.
What did I do wrong? Thanks.
The data is wrapped in iframe so you have to switch to the iframe first and then get the details.
driver.switch_to.frame(0)
driver.find_element_by_css_selector('text').text
Result:
'6,876'
Screenshot:
Please find solution
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver = webdriver.Chrome(executable_path=r"C:\New folder\chromedriver.exe")
url = 'https://www.nj.gov/health/cd/topics/covid2019_dashboard.shtml'
driver.get(url)
driver.maximize_window()
iframe = WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.TAG_NAME, 'iframe')))
driver.switch_to.frame(iframe)
svg = WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.CSS_SELECTOR, 'text')))
print svg.text
Related
I am trying to have selenium to do the following:
Open a website
Click on the search box
Type "Seattle" in the search box
Select the first result from the suggested results
Hit Enter
Click on the new search box
Type "Chicago" in the new search box
Select the first result from the suggested results
I was able to get it to work until step 5, but I can't find a way to do the same tasks with a new search box in step 6.
Here's 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
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 20)
url = 'https://wego.here.com/'
driver.get(url)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input.input_search"))).click()
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input.input_search"))).send_keys('Seattle')
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input.input_search"))).send_keys(Keys.ENTER)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "button.btn"))).send_keys(Keys.ENTER)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input.ng_pristine"))).click()
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input.ng_pristine"))).send_keys('Chicago')
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input.ng_pristine"))).send_keys(Keys.ENTER)
Here's what the source page of the new search box looks like:
This is the final result I want to see:
In this specific scenario, to make a new search you should first clear the previous search state.
This should work:
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
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 20)
url = 'https://wego.here.com/'
driver.get(url)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input.input_search"))).click()
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input.input_search"))).send_keys('Seattle')
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input.input_search"))).send_keys(Keys.ENTER)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "button.btn"))).send_keys(Keys.ENTER)
# clear the previous search results
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".btn_close"))).click()
#perform a new search
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input.input_search"))).click()
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input.input_search"))).send_keys('Chicago')
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input.input_search"))).send_keys(Keys.ENTER)
UPD
For the edited question the answer is:
You are using a wrong locator.
This should work:
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
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 20)
url = 'https://wego.here.com/'
driver.get(url)
search_input = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input.input_search")))
search_input.click()
search_input.send_keys('Seattle')
search_input.send_keys(Keys.ENTER)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "button.btn"))).send_keys(Keys.ENTER)
internal_search_input = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input#itinerary_item_input_0")))
internal_search_input.click()
internal_search_input.send_keys('Chicago')
internal_search_input.send_keys(Keys.ENTER)
I wrote this short programm to login in to my postfield:
import time
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
driver = webdriver.Chrome()
driver.maximize_window()
driver.get('https://www.web.de/')
time.sleep(2)
frame = driver.find_element_by_name('landingpage')
# do something with frame ...
driver.switch_to.frame(frame)
innerFrame = driver.find_element_by_tag_name("iframe")
driver.switch_to.frame(innerFrame)
driver.find_element_by_id("save-all-conditionally").click()
time.sleep(2)
driver.switch_to.default_content()
time.sleep(3)
inputemail = driver.find_element_by_xpath('/html/body/div[2]/div/div[2]/div[2]/section[1]/div/form/div[2]/div/input')
inputpwd = driver.find_element_by_xpath('/html/body/div[2]/div/div[2]/div[2]/section[1]/div/form/div[3]/div/input')
buttonsend = driver.find_element_by_xpath('/html/body/div[2]/div/div[2]/div[2]/section[1]/div/form/button')
inputemail.send_keys('xxx#web.de')
inputpwd.send_keys('xxx')
buttonsend.click()
time.sleep(3)
frame2 = driver.find_element_by_name('home')
driver.switch_to.frame(frame2)
linkwrite = driver.find_element_by_xpath('/html/body/div[3]/div/div[3]/div[2]/div[1]/div[6]/div[1]/ul/li/section/div[3]/div/div[1]/a')
linkwrite.click()
This part is working fine with the iframes there. My next goal was then to fill out an input field. The Code of the page which opened after the sign in progress is the one on the picture: https://www.transfernow.net/dl/20210919Porfd5N7
But the Code for filling:
frame3 = driver.find_element_by_xpath('...')
driver.switch_to.frame(frame3)
fillin = driver.find_element_by_xpath('/html/body/div[3]/div[3]/div[3]/div[1]/div[1]/div/form/div[2]/div[1]/div[2]/div[1]/div[1]/div[2]/div/div/ul/li/input')
fillin.send_keys('hello')
has resulted in:
"Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/nav/div[2]/div[1]/div[2]/a[1]"}"
Where's my mistake?
Please help me!
Few things
There are nested iframe, first one is iframe[name='landingpage'] (located by css selector) second one is iframe[sandbox*='allow-popups'][style^='display'] and then you can click on Zustimmen und weiter button.
The xpath that you are using is absolute, try using relative xpath, if possible switch to css_selector.
Use Explicit waits.
Code :-
driver = webdriver.Chrome(driver_path)
driver.maximize_window()
driver.implicitly_wait(30)
wait = WebDriverWait(driver, 20)
driver.get("https://web.de/consent-management/")
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[name='landingpage']")))
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[sandbox*='allow-popups'][style^='display']")))
wait.until(EC.element_to_be_clickable((By.ID, "save-all-conditionally"))).click()
driver.switch_to.default_content()
inputemail = wait.until(EC.element_to_be_clickable((By.ID, "freemailLoginUsername")))
inputpwd = wait.until(EC.element_to_be_clickable((By.ID, "freemailLoginPassword")))
buttonsend = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Login']")))
inputemail.send_keys('xxx#web.de')
inputpwd.send_keys('xxx')
buttonsend.click()
time.sleep(3)
Imports:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Additionally you can use the below code :
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.pos-input input"))).send_keys('WEB.DE E-Mail-Adresse')
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.pos-input input[name^='password']"))).send_keys('password here')
to fill details on Bitte erneut einloggen page.
I am trying to get selenium to click on a link for me but it cannot find any xpaths and I cannot figure out why
code:
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium import webdriver
driver = webdriver.Chrome(executable_path=r'/home/littlejiver/Downloads/chromedriver_linux64/chromedriver')
wait = WebDriverWait(driver, 10)
driver.get("https://thepiratebay3.org/")
wait.until(ec.element_to_be_clickable((By.XPATH, '//*[#id="input-search"]'))).send_keys("Toy Story")
wait.until(ec.element_to_be_clickable((By.XPATH, '//*[#id="pirate-search"]'))).click()
wait.until(ec.element_to_be_clickable((By.XPATH, '//div[#id="vpnModal"]//span[text()="No thanks"]'))).click()
wait.until(ec.element_to_be_clickable(
(By.XPATH,
'//*[#id="searchResult"]/tbody/tr[1]/td[2]/div/a'))).click()
I've tried several differnet Xpaths and they are not being found and I cannot figure out why (the last link is the isssue in #id="searchResult")
any help would be appreciated
thanks,
littlejiver
<iframe target="_self" class="search-i" src="https://tpb.party/search/Toy+Story" frameborder="0" width="100%" height="100%" scrolling="auto" sandbox="allow-forms allow-same-origin allow-popups allow-popups-to-escape-sandbox" allowfullscreen=""></iframe>\
Your element is under an iframe. Switch to it.
wait.until(ec.frame_to_be_available_and_switch_to_it((By.CLASS_NAME, 'search-i')))
Complete Code
wait = WebDriverWait(driver, 10)
driver.get("https://thepiratebay3.org/")
wait.until(ec.element_to_be_clickable((By.XPATH, '//*[#id="input-search"]'))).send_keys("Toy Story")
wait.until(ec.element_to_be_clickable((By.XPATH, '//*[#id="pirate-search"]'))).click()
wait.until(ec.element_to_be_clickable((By.XPATH, '//div[#id="vpnModal"]//span[text()="No thanks"]'))).click()
wait.until(ec.frame_to_be_available_and_switch_to_it((By.CLASS_NAME, 'search-i')))
wait.until(ec.element_to_be_clickable((By.XPATH,'//*[#id="searchResult"]/tbody/tr[1]/td[2]/div/a'))).click()
I'm just a beginner with programming and I'm trying to learn how to automate web procedures.
I am working based on https://www.coeweb.istat.it/.
I need to find elements of the page and I already tried
with driver.find_element_by_xpath
but since I'm not very familiar with html code, I'm not even sure to be using the right piece of code as argument for that method.
Can someone please send me as an example of code to click on the link qui. in bold right in the middle of the page please?
This is what I've tried:
from selenium import webdriver
from selenium.webdriver.common.by import By
url = "https://www.coeweb.istat.it/"
driver = webdriver.Chrome()
driver.get(url)
link = driver.find_element_by_xpath('//[#id="AutoNumber1"]/tbody/tr[3]/td[2]/p[3]/font/a/strong')
link.click()
Still, I get the error as:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id="AutoNumber1"]/tbody/tr[3]/td[2]/p[3]/font/a/strong"}
You have to wait until it will be clickable:
# I have fixed xpath
element = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//*[#id='AutoNumber1']//a[contains(., 'qui.')]"))
)
element.click()
Note: you have to do some imports:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
There is also possible to use CSS Selector to locate the element:
#AutoNumber1 > tbody > tr:nth-child(3) > td:nth-child(2) > p:nth-child(3) > font > a
and
element = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.CSS_SELECTOR, "#AutoNumber1 > tbody > tr:nth-child(3) > td:nth-child(2) > p:nth-child(3) > font > a"))
)
element.click()
EDIT: you have to switch to the frame before locate your element like this:
driver.switch_to.frame(driver.find_element_by_xpath("//frame[#name = 'principale']"))
So the full code will be like this:
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
url = "https://www.coeweb.istat.it/"
driver = webdriver.Chrome()
driver.get(url)
driver.switch_to.frame(driver.find_element_by_xpath("//frame[#name = 'principale']")) # switches to frame
element = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//*[#id='AutoNumber1']//a[contains(., 'qui.')]"))
)
element.click()
driver.switch_to.default_content() # switch back to default content
Explanation: you cannot interact with the elements which are in iframe or in a frame. To be able to do this, you have to find a frame, switch to it, do stuff and then switch back to default content
As per the URL you have shared the link with text as qui. is withan a <frame>. So you have to switch to the desired frame inducing WebDriverWait and then again induce WebDriverWait while looking out for the element as follows:
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
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_argument('disable-infobars')
driver=webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
url = "https://www.coeweb.istat.it/"
driver.get(url)
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.NAME,"principale")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//table[#id='AutoNumber1']//tr//td//p//a[#href='lnkdati.htm' and .//strong[contains(.,'qui.')]]"))).click()
Browser Snapshot:
I am trying to login to the ESPN website using selenium. Here is my code thus far
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
driver = webdriver.Firefox()
driver.maximize_window()
url = "http://www.espn.com/fantasy/"
driver.get(url)
login_button = driver.find_element_by_xpath("/html/body/div[6]/section/section/div/section[1]/div/div[1]/div[2]/a[2]")
login_button.click()
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "/html/body/div[2]/div/div/section/section/form/section/div[1]/div/label/span[2]/input")))
except:
driver.quit()
Basically, there are 2 steps, first I have to click the login button and then I have to fill in the form. Currently, I am clicking the login button and the form is popping up but then I can't find the form. I have been using firebug to get the xpath as suggested in other SO questions. I don't really know much about selenium so I am not sure where to look
Try to use
driver.switch_to_frame('disneyid-iframe')
# handle authorization pop-up
driver.switch_to_default_content() # if required
This works for me, switching to the iframe first. Note that you will need to switch back out of the iframe after entering the credentials.
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
driver = webdriver.Firefox()
driver.maximize_window()
url = "http://www.espn.com/fantasy/"
driver.get(url)
login_button = driver.find_element_by_xpath("/html/body/div[6]/section/section/div/section[1]/div/div[1]/div[2]/a[2]")
login_button.click()
iframe = driver.find_element_by_id("disneyid-iframe")
driver.switch_to.frame(iframe)
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "/html/body/div[2]/div/div/section/section/form/section/div[1]/div/label/span[2]/input")))
element.send_keys("my username")
import time
time.sleep(100)
finally:
driver.quit()