Why does this works only sometimes? Sometimes it navigates easily and sometimes it gets stuck after selecting a category. I want to click the category button and select notebook then the series button and click "acer one" and then the first model from model menu. But somehow it gets stuck sometimes after category and sometimes after series.
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.support.select import Select
import time
import urllib.request
PATH = r"D:\Py\Selenium\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get('https://www.acer.com/ac/en/GB/content/support')
driver.execute_script("window.scrollTo(0, document.body.scrollHeight/5);")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[7]/div[3]/div/div[3]/div/div[1]/a"))).click()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[7]/div[3]/div/div[3]/div/div[1]/ul/li[1]/a"))).click()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[7]/div[3]/div/div[3]/div/div[2]/a"))).click()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[7]/div[3]/div/div[3]/div/div[2]/ul/li[2]"))).click()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[7]/div[3]/div/div[3]/div/div[3]/a"))).click()
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"/html/body/div[7]/div[3]/div/div[3]/div/div[3]/ul/li[1]"))).click()
src = WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH,"/html/body/div[7]/div[2]/div/div/div[1]/img"))).get_attribute("src")
urllib.request.urlretrieve(str(src),"img.png")
time.sleep(2)
driver.quit()
# search.send_keys("m")
I'm really not sure why your code is failing, but i have tried this code which i ran multiple times and it is not failing. Please give it a try and do let me know if you have any issues running this script. Please find the code below -
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
import time
import urllib.request
driver = webdriver.Chrome()
driver.get('https://www.acer.com/ac/en/GB/content/support')
wait = WebDriverWait(driver, 30)
wait.until(EC.element_to_be_clickable((By.XPATH, "//a[contains(text(),'Category')]"))).click()
wait.until(EC.element_to_be_clickable(
(By.XPATH, "//a[contains(text(),'Category')]/following-sibling::ul/li/a[text()='Notebook']"))).click()
wait.until(EC.element_to_be_clickable((By.XPATH, "//a[contains(text(),'Series')]"))).click()
wait.until(EC.element_to_be_clickable(
(By.XPATH, "//a[contains(text(),'Series')]/following-sibling::ul/li/a[text()='Acer One']"))).click()
wait.until(EC.element_to_be_clickable((By.XPATH, "//a[contains(text(),'Model')]"))).click()
wait.until(EC.element_to_be_clickable(
(By.XPATH, "//a[contains(text(),'Model')]/following-sibling::ul/li[1]"))).click()
src = wait.until(
EC.element_to_be_clickable((By.XPATH, "/html/body/div[7]/div[2]/div/div/div[1]/img"))).get_attribute("src")
urllib.request.urlretrieve(str(src), "img.png")
time.sleep(2)
driver.quit()
I have slightly modified your code. Thank You..!
Related
I am going to scrap glassdoor review for a list of companies. To do this, I need to first login in order to get access all reviews. Then, my approach is entering the name of the 1st company and then scarp its review.....do the same for all companies in the list....
To do this when I go to this URL "https://www.glassdoor.co.in/member/home/index.htm", I should insert the name of company in "search text-box" and then select first index from list view and finally enter search button to go the next link which is review of that comapny..My challenge is with selecting 1st index from "search textbox".. Actually, the last line of code where I going to send cursor on "search button" . I have this error "AttributeError: 'NoneType' object has no attribute 'send_keys'"
I would appreciate if you could help me!
I used the following 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
from selenium.common.exceptions import TimeoutException, WebDriverException
from selenium.webdriver.chrome.options import Options
import time
import pandas as pd
driver_path= r"C:\Users\TMaghsoudi\Desktop\chromedriver_win32.exe"
# chrome options
options = webdriver.ChromeOptions()
# options.add_argument("--start-maximized")
options.add_argument('--ignore-certificate-errors')
options.add_argument('--ignore-ssl-errors')
options.add_experimental_option('excludeSwitches', ['enable-logging'])
driver = webdriver.Chrome(driver_path, chrome_options=options)
# set driver
driver = webdriver.Chrome(driver_path, chrome_options=options)
# get url
url = "https://www.glassdoor.co.in/Job/index.htm"
driver.get(url)
time.sleep(3)
driver.find_element(By.CLASS_NAME, "HeaderStyles__signInButton").click()
# singin = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.ID, "SignInButton")))
# singin.click()
time.sleep(5)
Enter_email= driver.find_element(By.ID, "modalUserEmail")
Enter_email.send_keys("XXXXXX")
Enter_email.send_keys(Keys.ENTER)
Enter_pass= driver.find_element(By.ID,"modalUserPassword")
Enter_pass.send_keys("XXXXX")
SingIn= WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//div[#class='d-flex align-items-center flex-column']/button[#class='gd-ui-button mt-std minWidthBtn css-1dqhu4c evpplnh0']")))
SingIn.click()
time.sleep(5)
driver.set_window_size(1120, 1000)
driver.find_element(By.CLASS_NAME,"siteHeader__HeaderStyles__navigationItem:nth-child(2)").click()
Company=driver.find_element(By.ID,"sc\.keyword").send_keys("Amazon")
Company.send_keys(Keys.ENTER) **#the error is here!!!!!!**
By running
Company = driver.find_element(By.ID,"sc\.keyword").send_keys("Amazon")
you are assigning the value None to Company, this is because .send_keys() returns the value None if it executed without errors.
So you have to run
Company=driver.find_element(By.ID,"sc\.keyword")
Company.send_keys("Amazon")
Company.send_keys(Keys.ENTER)
You can also run the last two commands in one line
Company.send_keys("Amazon" + Keys.ENTER)
To first write within the search box and then instead of selecting the first index in Listview, you could send ENTER back to back using the following locator strategies:
Code block:
from selenium.webdriver.common.keys import Keys
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('https://www.glassdoor.co.in/Job/index.htm')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#class='d-none d-lg-block']//button[text()='Sign In']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#modalUserEmail"))).send_keys("debanjan.selenium#gmail.com")
driver.find_element(By.CSS_SELECTOR, "input#modalUserPassword").send_keys("dev_anjan")
driver.find_element(By.XPATH, "//span[text()='Sign In']").click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[aria-label='Search Keyword']"))).send_keys("Amazon" + Keys.RETURN)
Browser Snapshot:
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'm attempting to click the next page button on this site. I've looked at multiple proposed solutions on Stack Overflow for similar issues, but none of them seem to work. This is my current code as of now:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 10)
next_button = wait.until(EC.element_to_be_clickable((By.XPATH, "html/body/div/div/div/main/div[3]/div[2]/div/div[3]/div[1]/div[2]/div[2]/div/div[1]/div[3]/button[2]/i/svg")))
driver.execute_script("return arguments[0].scrollIntoView();", next_button)
next_button.click()
Any help would be appreciated. Thank you!
I have written this code to replicate your scenario and it is working fine. I have used the xpath which search for svg element with some class name. The problem with the website is, there are many duplicate elements present in it. Because of that might be your code is not working. Please try below code.
from selenium import webdriver
from selenium.webdriver import ActionChains
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")
driver = webdriver.Chrome(options=options)
driver.get('https://rotogrinders.com/resultsdb/site/draftkings/date/2018-07-02/sport/mlb/slate/'
'5b3aafe73870401e20652804/contest/5b3aafe83870401e20652b84')
wait = WebDriverWait(driver, 30)
# Find Next Button and Click
Next_Button = wait.until(EC.element_to_be_clickable(
(By.XPATH, "(//*[local-name()='svg' and #data-icon='right'])[2]/ancestor::button")))
Next_Button.click()
Do let me know if you have any queries?
You may try as follows:
wait = WebDriverWait(driver, 10)
next_button = wait.until(EC.element_to_be_clickable((By.XPATH, '(//*[#class="anticon anticon-right"])[2]')))
driver.execute_script("return arguments[0].scrollIntoView();", next_button)
next_button.click()
I am currently trying to learn selenium in Python and I am having an issue clicking the "Accept All Cookies" button.
I am using:
Python v3.9
Chrome v87
This is the HTML page i am trying to scrape
https://www.currys.co.uk/gbuk/tv-and-home-entertainment/televisions/televisions/samsung-ue75tu7020kxxu-75-smart-4k-ultra-hd-hdr-led-tv-10213562-pdt.html
Here is my code currently
# Selenium Tutorial #1
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
import time
driver = webdriver.Chrome(r"C:\Users\Ste1337\Desktop\chromedriver\chromedriver.exe")
driver.get("https://www.currys.co.uk/gbuk/tv-and-home-entertainment/televisions/televisions/samsung-ue75tu7020kxxu-75-smart-4k-ultra-hd-hdr-led-tv-10213562-pdt.html")
#search = driver.find_element_by_id(ContentPlaceHolder1_NotifyBtn)
driver.implicitly_wait(10)
link = driver.find_element_by_id("onetrust-accept-btn-handler")
link.click
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "onetrust-accept-btn-handler"))
)
element.click
except:
driver.quit()
The "( )" is missing after the click.
Try this:
link = driver.find_element_by_id("onetrust-accept-btn-handler")
link.click()
Simply wait and click.
wait = WebDriverWait(driver, 10)
driver.get("https://www.currys.co.uk/gbuk/tv-and-home-entertainment/televisions/televisions/samsung-ue75tu7020kxxu-75-smart-4k-ultra-hd-hdr-led-tv-10213562-pdt.html")
wait.until(EC.element_to_be_clickable((By.ID, "onetrust-accept-btn-handler"))).click()
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()