I'm trying selenium for the first time. My code is as follows:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
import selenium.webdriver.support.ui as ui
import selenium.webdriver.support.expected_conditions as EC
import os
import time
class expediaUnitTest():
def __init__(self):
options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_argument('--ignore-ssl-errors')
dir_path=os.getcwd()
chromedriver=dir_path+"\chromedriver"
os.environ["webdriver.chrome.driver"]=chromedriver
driver=webdriver.Chrome(chrome_options=options,executable_path=chromedriver)
def timerPractice(self):
time.sleep(5)
def gotoexpedia(self):
self.driver.get("https://www.expedia.com/")
def teardown(self):
self.driver.close()
if __name__=="__main__":
obj=expediaUnitTest()
obj.gotoexpedia()
A new chromebrowser is called but it does not access the webpage. I get the errormessage:
AttributeError: 'expediaUnitTest' object has no attribute 'driver'
When i give the timePractise(), it works perfectly,as in the browser disappears after the given number of seconds. But it does not seem to be calling a function.
Ps: I'm following the online tutorial given here:https://www.youtube.com/watch?v=zZjucAn_JYk
He doesn't have the problem that I'm having.
Can someone please help?
You are missing self when creating the instance of the driver. So instead of
driver=webdriver.Chrome(chrome_options=options,executable_path=chromedriver)
it should be
self.driver=webdriver.Chrome(chrome_options=options,executable_path=chromedriver)
(in the video they are doing it exactly that way)
Related
I am trying to click the [+] Button in the INSSIST extension using selenium, but I just get an error everytime. I just want to know if it is possible to click it. Can't figure it out
For the code to work you have to make sure you got the chrome inssist extension and you have close all instances of chrome otherwise it will not load selenium correctly, because it is a background extension aswell
Here is the online instagram alternative called inssist
https://chrome.google.com/webstore/detail/inssist-web-client-for-in/bcocdbombenodlegijagbhdjbifpiijp
from selenium import webdriver
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 as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
import time
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
chrome_options.add_argument(r"user-data-dir=C:\\Users\\Alexander Smith\\AppData\\Local\\Google\\Chrome\\User Data")
chrome_options.add_argument('profile-directory=Profile 2')
driver = webdriver.Chrome(executable_path=r"C:\\Users\\Alexander Smith\\Desktop\\chromedriver.exe", options=chrome_options)
driver.get("chrome-extension://bcocdbombenodlegijagbhdjbifpiijp/inssist.html#instagram.com/")
print("Good")
create = WebDriverWait(driver,9).until(EC.element_to_be_clickable((By.XPATH, '//\*\[#id="mount_0_0_3c"\]/div/div/div/div\[1\]/div/div/div/div\[1\]/div\[1\]/div\[1\]/div/div/div/div/div\[1\]/div')))
print("Good")
ActionChains(driver).move_to_element(create)
print("Good")
create.send_keys(Keys.RETURN)
I need a way to automatically scroll a browser window that i've opened using selenium code. I am currently using pyautogui but this requires the window to be selected. This is running on a virtual machine that i'm remoting into and when i toggle off the VM to work on my main desktop the code does not scroll so i need a way to auto scroll on a non active window.
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
import numpy as np
from pynput.mouse import Button, Controller
import win32api
import win32con
import pyautogui
import pandas as pd
import re
from selenium.common.exceptions import TimeoutException
pyautogui.FAILSAFE = False
s = Service(r'\\homedirpva1a01\USERSNC$\603225\Edgedriver\msedgedriver.exe')
browser = webdriver.Edge(service=s)
Options.page_load_strategy = 'eager'
time.sleep(5)
linklist=[]
browser.get('https://turo.com/us/en/search?country=US&defaultZoomLevel=11&delivery=false&deliveryLocationType=googlePlace&endDate=01%2F25%2F2023&endTime=10%3A00&isMapSearch=false&itemsPerPage=200&latitude=40.735657&location=Newark%2C%20New%20Jersey%2C%20USA&locationType=CITY&longitude=-74.1723667&pickupType=ALL&placeId=ChIJHQ6aMnBTwokRc-T-3CrcvOE®ion=NJ&sortType=RELEVANCE&startDate=01%2F22%2F2023&startTime=10%3A00&useDefaultMaximumDistance=true')
time.sleep(3)
print('start4434')
i4434=1
elems4434=[]
while i4434<50:
if 'An error occurred while searching, please try again' in browser.page_source:
listsave=['i4434','i4434']
save = pd.DataFrame(listsave)
save.to_csv(r'\\PATH\savetest.csv')
exit()
elif 'vehicle-card-link-box' not in browser.page_source:
listsave=['i4434','i4434']
save = pd.DataFrame(listsave)
save.to_csv(r'\\PATH\savetest.csv')
break
else:
elems4434=browser.find_elements(By.XPATH, '//a[#href]')
distinctlist = list(set(linklist))
frame = pd.DataFrame(distinctlist)
frame.to_csv(r'\\PATH\turolinklisttest.csv')
listsave=['i4434','i4434']
save = pd.DataFrame(listsave)
save.to_csv(r'\\PATH\savetest.csv')
for elem in elems4434:
if 'car-rental/united-states' in elem.get_attribute('href'):
linklist.append(elem.get_attribute('href'))
if 'suv-rental/united-states' in elem.get_attribute('href'):
linklist.append(elem.get_attribute('href'))
pyautogui.scroll(-1000)
time.sleep(3)
i4434+=1`
I have this website am trying to click on a button in selenium python but it keeps telling me it cant locate the element both implicitly and explicit wait could not make it work. Please any guidance would be appreciated.
Below is what i have done:
import time
import unittest
from selenium.webdriver import ActionChains
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
from selenium.common import NoSuchElementException
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.support.wait import WebDriverWait
class TestButton(unittest.TestCase):
def test_button(self):
self.driver = webdriver.Chrome()
self.driver.get('https://www.moneyhelper.org.uk/en/money-troubles/coronavirus/use-our-money-navigator-tool')
time.sleep(3)
"""clicking to close a cooking popup"""
self.driver.find_element(By.XPATH, '//*[#id="ccc-notify-accept"]').click()
time.sleep(3)
"""the button which is an anchor tag i have been trying to click"""
self.driver.find_element(By.XPATH, "//div[#class='landing__actions']//a").click()
time.sleep(5)
def tearDown(self):
self.driver.quit()
if __name__ == "__main__":
unittest.main()
There is an iframe before //div[#class='landing__actions']//a and you need to switch to this iframe
just try it:
"""clicking to close a cooking popup"""
self.driver.find_element(By.XPATH, '//*[#id="ccc-notify-accept"]').click()
time.sleep(3)
iframe = self.driver.find_element(By.XPATH, '//iframe[#title="Money Navigator tool"]')
self.driver.switch_to.frame(iframe)
"""the button which is an anchor tag i have been trying to click"""
self.driver.find_element(By.XPATH, "//div[#class='landing__actions']//a").click()
time.sleep(5)
It's inside a iframe, you need switch it first using .switch_to.frame(iframe reference):
...
iframe = self.driver.find_element(By.CSS_SELECTOR, '.cmp-embed__iframe')
"""the button which is an anchor tag i have been trying to click"""
self.driver.switch_to.frame(iframe)
self.driver.find_element(By.XPATH, "//div[#class='landing__actions']//a").click()
time.sleep(5)
I am trying to scrape all company names from inc5000 site ("https://www.inc.com/inc5000/2021"). The problem is that the company names are displayed using JavaScript. I have tried using selenium and requests_html both to render the site but still when I fetch source code of page i get JavaScript. This is what I tried. I am new to web scraping so it is possible that I am making some foolish mistake. please guide
Here is my code.
...
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
from bs4 import BeautifulSoup
options = Options()
options.headless = True
driver = webdriver.Chrome(ChromeDriverManager().install(),options=options)
driver.get("https://www.inc.com/inc5000/2021")
data=driver.page_source
print(data)
...
You could give some time to render or use seleniums waits:
...
import time
driver.get('https://www.inc.com/inc5000/2021')
time.sleep(5)
data = driver.page_source
soup = BeautifulSoup(data)
for e in soup.select('.company'):
print(e.text)
...
Why do you need beautiful soup, you just could use selenium:
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("https://www.inc.com/inc5000/2021")
companies = [e.text for e in driver.find_elements(By.CLASS_NAME, "company")]
This will only give you the elements in the viewport. You need to improve on that by scrolling.
I want to keep chrome open and close the console. I've read that webdriver.service.stop() will do this but I can't get it to work. I've tried importing several things from selenium that are namec service but I stil get selenium.webdriver has no attribute'service'. The last two imports are unused. Currently when I run the script without pycharmt the console starts and if I close it after it runs it closes the chrome window as well. I would like to not have to manually close the console window.
This is my updated code as of 2019-03-15:
from selenium import webdriver
from selenium.webdriver import ChromeOptions
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.common import service
from selenium.webdriver.ie.service import service
chrome_options = ChromeOptions()
chrome_options.add_argument("--incognito")
chrome_options.add_argument("--start-maximized")
chrome_options.set_capability("detach", True)
DynamoForum = webdriver.Chrome(chrome_options=chrome_options)
DynamoForum.get("https://forum.dynamobim.com/")
Parent_window = DynamoForum.window_handles[0]
login = DynamoForum.find_element_by_class_name("header-buttons").click()
wait = WebDriverWait(DynamoForum, 20)
window_child = DynamoForum.window_handles[1]
DynamoForum.switch_to.window(window_child)
wait.until(EC.visibility_of_element_located((By.ID, "userName"))).send_keys("abc#abc.com")
DynamoForum.find_element_by_id("verify_user_btn").click()
wait.until(EC.visibility_of_element_located((By.ID, "password"))).send_keys("abc")
DynamoForum.find_element_by_id("btnSubmit").click()
try:
DynamoForum.service.stop()
except AttributeError:
pass
Below is the old Code:
from selenium import webdriver
from selenium.webdriver import ChromeOptions
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.common import service
from selenium.webdriver.ie.service import service
chrome_options = ChromeOptions()
chrome_options.add_argument("--incognito")
DynamoForum = webdriver.Chrome(chrome_options=chrome_options)
DynamoForum.get("https://forum.dynamobim.com/")
Parent_window = DynamoForum.window_handles[0]
login = DynamoForum.find_element_by_class_name("header-buttons").click()
wait = WebDriverWait(DynamoForum, 20)
window_child = DynamoForum.window_handles[1]
DynamoForum.switch_to.window(window_child)
wait.until(EC.visibility_of_element_located((By.ID,
"userName"))).send_keys("abc#abc.com")
DynamoForum.find_element_by_id("verify_user_btn").click()
wait.until(EC.visibility_of_element_located((By.ID,
"password"))).send_keys("abc")
DynamoForum.find_element_by_id("btnSubmit").click()
webdriver.service.stop()
This is the error I'm getting. This has been solved by changing webdriver.serive.stop to DynamoForum.service.stop.
C:/Users/cjr/PycharmProjects/DynamoForum/DynamoForum.py:13:
DeprecationWarning: use options instead of chrome_options
DynamoForum = webdriver.Chrome(chrome_options=chrome_options)
Traceback (most recent call last):
File "C:/Users/cjr/PycharmProjects/DynamoForum/DynamoForum.py", line 29,
in <module>
webdriver.service.stop()
AttributeError: module 'selenium.webdriver' has no attribute 'service'
This is workaround only
I know this is not the correct approach, but for time being you can replace the last line with below. This will pass the execution, but keep your browser open after execution.
try:
webdriver.service.stop()
except AttributeError:
pass
Let me know if this is helpful. Will keep digging into this in the mean time.