Exception has occurred: AttributeError 'WebDriver' object has no attribute 'link' - python

I was doing an availability checker for some Amazon products, it was working but after I went for sometime I got back and I found this error. I don't know if I've missed something or editing something.
I hope the following information helps:
This is the error:
Exception has occurred: AttributeError 'WebDriver' object has no attribute 'link'
and this is my code:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import webbrowser
import time
import os
urls = [
"URL 1",
"URL 2"
]
PATH = "C:\Program Files (x86)\chromedriver.exe"
options = Options()
options.add_argument('--headless')
options.add_argument('--disable-gpu')
driver = webdriver.Chrome(PATH, chrome_options=options)
def main():
# stuff
while True:
for i in range(0, len(urls)):
url = urls[i]
time.sleep(2)
print(url)
wait = WebDriverWait(driver, 10)
driver.link(url)
wait.until(EC.element_to_be_clickable((By.XPATH, '//*[#id="availability"]/span')))
print(driver.title)
availablility = driver.find_element_by_xpath('//*[#id="availability"]/span')
print(availablility.text)
main()
I am using Python 3.7.9 64-bit in Visual Studio Code.

There is no webdriver.link. You probably meant to use webdriver.get:
So just change driver.link(url)
into driver.get(url)

Related

Can't find element by name using selenium

I'm using selenium 4.7.2 and can't find the element by its name. The following code returns NoSuchElementException error:
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
# Get the website using the Chrome webbdriver
browser = webdriver.Chrome()
browser.get('https://www.woofshack.com/en/cloud-chaser-waterproof-softshell-dog-jacket-ruffwear-rw-5102.html')
# Print out the result
price = browser.find_element(By.NAME, 'data-price-665')
print("Price: " + price.text)
# Close the browser
time.sleep(3)
browser.close()
What's wrong in using find_element method?
Looks like you are using a wrong locator here. I see no element with name attribute value 'data-price-665' on that page.
The following code is working:
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument("start-maximized")
webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(service=webdriver_service, options=options)
wait = WebDriverWait(driver, 20)
actions = ActionChains(driver)
url = "https://www.woofshack.com/en/cloud-chaser-waterproof-softshell-dog-jacket-ruffwear-rw-5102.html"
driver.get(url)
price = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "#product-price-665 .price")))
print("Price: " + price.text)
The output is:
Price: €112.95

cannot click element in python via selenium as a variable

I want to save element data to an excel file via python. I have the code below, I need some help why the line where
element.click()
gives an error. Even though I put the click() method upper line, but i need it to be in line below.
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome(r"C:\Users\Admin\Downloads\chromedriver_win32 (1)\chromedriver.exe")
driver.get("https://www.nba.com/schedule?pd=false&region=1")
driver.implicitly_wait(30)
element_to_click=driver.find_element(By.ID,"onetrust-accept-btn-handler").click()
element_to_click.click() 'error
element_to_save=driver.find_element(By.XPATH,"//div/div/div/div/h4")
#element_to_save.to_excel("3row,3column)")
driver.quit()
This is one way to reject/accept cookies on that website:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
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
chrome_options = Options()
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument('disable-notifications')
chrome_options.add_argument("window-size=1280,720")
webdriver_service = Service("chromedriver/chromedriver") ## path to where you saved chromedriver binary
browser = webdriver.Chrome(service=webdriver_service, options=chrome_options)
wait = WebDriverWait(browser, 20)
url = 'https://www.nba.com/schedule?pd=false&region=1'
browser.get(url)
try:
wait.until(EC.element_to_be_clickable((By.ID, "onetrust-accept-btn-handler"))).click()
print('accepted cookies')
except Exception as e:
print('no cookie button!')
Setup is selenium/chrome on linux - just observe the imports and the part after defining the browser/driver.
Selenium documentation can be found at https://www.selenium.dev/documentation/

Selenium can't get a page with punycode url

Here's how I'm getting page content
from selenium.webdriver.support.wait import WebDriverWait
import os
from seleniumwire import webdriver
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.options import Options
from seleniumwire.handler import log as seleniumLog
from seleniumwire.server import logger as selenium_server_log
from webdriver_manager.firefox import GeckoDriverManager
options = Options()
options.add_argument('--user-agent="' + user_agent + '"')
options.add_argument("--start-maximized")
options.add_argument("--headless")
driver = webdriver.Firefox(
executable_path=GeckoDriverManager().install(),
options=options,
)
driver.set_page_load_timeout(30)
try:
driver.get('https://xn--e1aicoccdeejjbbl0l.xn--p1ai/uslugi/stroitelstvo/price/')
WebDriverWait(driver, 40).until(ec.presence_of_element_located((By.TAG_NAME, "html")))
except Exception as e:
error = True
print(e)
This is the output I have:
Message: Reached error page: about:neterror?e=dnsNotFound&u=https%3A//xn--e1aicoccdeejjbbl0l.xn--p1ai/uslugi/stroitelstvo/price/&c=UTF-8&d=We%20can%E2%80%99t%20connect%20to%20the%20server%20at%20xn--e1aicoccdeejjbbl0l.xn--p1ai.
When I try to get a content from usual latinic url, everything works ok. The problem occurs when I use cyrillic or punycode urls.
What can I do about it?

How to download file via Beautifulsoup in aspx?

I want to download pdf files on the website using beautiful soup and selenium.
I've written the code up to here and it's incomplete. However, since I can't find the link to download the pdf file.
#!/usr/bin/python
from bs4 import BeautifulSoup
from selenium import webdriver
import webbrowser
import os
import requests
import urllib2
import time
import urllib
try:
options = webdriver.ChromeOptions()
options.add_argument("--headless")
options.add_argument('--no-sandbox')
driver = webdriver.Chrome("/usr/bin/chromedriver", chrome_options=options)
except urllib2.HTTPError as e:
print(e)
except urllib2.URLError:
print ("Server down or incorrect domains.")
else:
def not_relative_uri(href):
return re.compile('^https://').search(href) is not None
driver.get("https://xxxxxx")
# print(driver.page_source.encode('utf-8'))
my_folder="/home/python/"
soup_res = BeautifulSoup(driver.page_source.encode('utf-8'), 'html.parser')
tr = soup_res.find("div", {"id":"pageWrapper"}).find("div", {"class":"EGZDefault-List"}).find("div", {"class":"EGZDefault-List-Info-List"}).find("table", {"class":"gridview"}).find("tbody").find_all('tr')[1:21]
I hope someone can help me.
With Selenium you can do it as following:
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("/usr/bin/chromedriver", chrome_options=options)
wait = WebDriverWait(driver, 20)
actions = ActionChains(driver)
driver.get("https://xxxxxx")
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "table.gridview input[type='image']")))
time.sleep(2)
images = driver.find_elements_by_css_selector("table.gridview input[type='image']")
for image in images:
actions.move_to_element(image).perform()
time.sleep(0.5)
image.click()
time.sleep(5)

TypeError: 'FirefoxWebElement' object is not iterable [duplicate]

This question already has answers here:
TypeError: 'WebElement' object is not iterable error
(2 answers)
Closed 4 years ago.
I would like to get URLs of Airbnb's listing pages by Python, selenium, firefox, however, my program doesn't work well.
My error code is as bellow;
Original exception was:
Traceback (most recent call last):
File "pages.py", line 19, in <module>
for links in driver.find_element_by_xpath('//div[contains(#id, "listing-")]//a[contains(#href, "rooms")]'):
TypeError: 'FirefoxWebElement' object is not iterable
Here is my code!
from selenium import webdriver
from selenium.webdriver import FirefoxOptions
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
test_url = 'https://www.airbnb.jp/s/%E6%97%A5%E6%9C%AC%E6%B2%96%E7%B8%84%E7%9C%8C/homes?refinement_paths%5B%5D=%2Fhomes&query=%E6%97%A5%E6%9C%AC%E6%B2%96%E7%B8%84%E7%9C%8C&price_min=15000&allow_override%5B%5D=&checkin=2018-07-07&checkout=2018-07-08&place_id=ChIJ51ur7mJw9TQR79H9hnJhuzU&s_tag=z4scstF7'
opts = FirefoxOptions()
opts.add_argument("--headless")
driver = webdriver.Firefox(firefox_options=opts)
driver.get(test_url)
driver.implicitly_wait(30)
for links in driver.find_element_by_xpath('//div[contains(#id, "listing-")]//a[contains(#href, "rooms")]'):
listing_url = links.get_attribute('href')
print(listing_url)
driver.quit()
I tried to change my code, another code is as bellow;
(Error message is same as my first code.)
from selenium import webdriver
from selenium.webdriver import FirefoxOptions
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
test_url = 'https://www.airbnb.jp/s/%E6%97%A5%E6%9C%AC%E6%B2%96%E7%B8%84%E7%9C%8C/homes?refinement_paths%5B%5D=%2Fhomes&query=%E6%97%A5%E6%9C%AC%E6%B2%96%E7%B8%84%E7%9C%8C&price_min=15000&allow_override%5B%5D=&checkin=2018-07-07&checkout=2018-07-08&place_id=ChIJ51ur7mJw9TQR79H9hnJhuzU&s_tag=z4scstF7'
opts = FirefoxOptions()
opts.add_argument("--headless")
driver = webdriver.Firefox(firefox_options=opts)
driver.get(test_url)
driver.implicitly_wait(30)
links = driver.find_element_by_xpath('//a[contains(#href, "rooms")]')
for link in links:
listing_url = link.get_attribute('href')
print(listing_url)
driver.quit()
I am glad to you reply if you have a time.
Thank you.
You need to use find_elements_by_xpath where return a list of elements
Not find_element_by_xpath that returned only one element
...
links = driver.find_elements_by_xpath('//div[contains(#id, "listing-")]//a[contains(#href, "rooms")]')
for link in links:
print(link.get_attribute('href')
...
Output
https://www.airbnb.jp/rooms/7793811?location=%E6%97%A5%E6%9C%AC%E6%B2%96%E7%B8%84%E7%9C%8C&check_in=2018-07-07&check_out=2018-07-08
https://www.airbnb.jp/rooms/7793811?location=%E6%97%A5%E6%9C%AC%E6%B2%96%E7%B8%84%E7%9C%8C&check_in=2018-07-07&check_out=2018-07-08
...

Categories

Resources