Xpath grabbing wrong text - selenium

I'm trying to grab the price from this page: https://www.eq3.com/ca/en/product/cjv8cke45026q01786nahx8uf/lighting/lighting/pendant-lamps/nelson-bell-bubble-pendant-lamp?cjv49km02036401865sece5be=cjv49km0503650186hc90zvnq
It should be getting $1,376.15 CAD however I'm getting some other text from the page and sometimes it doesn't work at all and gives me:
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
Here's my code:
DRIVER_PATH = '/usr/bin/chromedriver'
options = webdriver.ChromeOptions()
options.add_argument("--headless")
driver = webdriver.Chrome(executable_path=DRIVER_PATH, options=options)
XPATH = '//*[#id="app"]/main/div/div/section[1]/div/div[3]/div/span[1]'
url = 'https://www.eq3.com/ca/en/product/cjv8cke45026q01786nahx8uf/lighting/lighting/pendant-lamps/nelson-bell-bubble-pendant-lamp?cjv49km02036401865sece5be=cjv49km0503650186hc90zvnq'
driver.get(url)
price = WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.XPATH, XPATH)))
print(price)
driver.quit()

Try that out:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support import expected_conditions as EC
DRIVER_PATH = '/usr/bin/chromedriver'
options = Options()
options.add_argument('--headless')
driver = webdriver.Chrome(executable_path=DRIVER_PATH, options=options)
url = "https://www.eq3.com/ca/en/product/cjv8cke45026q01786nahx8uf/lighting/lighting/pendant-lamps/nelson-bell-bubble-pendant-lamp?cjv49km02036401865sece5be=cjv49km0503650186hc90zvnq"
driver.get(url)
el = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//span[contains(#class,'MuiTypography-root') and contains(#class,'MuiTypography-h3')][1]")))
text = el.text
print(text)
driver.quit()

Related

Unable to locate element of save button

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
import time
from selenium.common.exceptions import NoSuchElementException,TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support import expected_conditions
driver = webdriver.Chrome(executable_path = r"C:\Users\mvintee\project_for_selenium\chromedriver_win32\chromedriver.exe")
actions = ActionChains(driver)
driver.get("http://pis033/NorthStarWMS/User/PurchaseOrders.aspx?t=1&PO=1")
driver.maximize_window()
#driver.implicitly_wait(100)
print(driver.title)
driver.find_element("id","Login1_UserName").send_keys("wms")
driver.find_element("id","Login1_Password").send_keys("1")
driver.find_element("class name","rbText").click()
element_to_hover_over=driver.find_element("xpath",'//*[#id="ctl00_MenuTabNew"]/ul/li[4]/span/span')
time.sleep(5)
hover = ActionChains(driver).move_to_element(element_to_hover_over)
hover.perform()
time.sleep(5)
driver.find_element("xpath", '//*[#id="ctl00_MenuTabNew"]/ul/li[4]/div/ul/li[1]/a/span').click()
time.sleep(5)
driver.find_element("id","ctl00_ContentPlaceHolder1_ShipOrderGrid_ctl00_ctl02_ctl00_btnAdd").click()
element=driver.find_element("id",'btnSave')
time.sleep(5)
element.click()
driver.quit()
Nothing happen when i locate element of save button. Rest of the code is working correct. I have tried all most everything but still locators not working.

Automating web application in selenium with python

This is my code where all things are alright but at last when i click on save button its not working. I have tried accessing element by id, name, xpath and class name of the save button but click for save not working. When that part need to executed nothing happen.Rest of the program executing properly.Plz suggest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
driver = webdriver.Chrome(executable_path = r"C:\Users\mvintee\project_for_selenium\chromedriver_win32\chromedriver.exe")
actions = ActionChains(driver)
driver.get("http://pis033/NorthStarWMS/User/PurchaseOrders.aspx?t=1&PO=1")
print(driver.title)
driver.find_element("id","Login1_UserName").send_keys("wms")
driver.find_element("id","Login1_Password").send_keys("1")
driver.find_element("class name","rbText").click()
time.sleep(5)
element_to_hover_over=driver.find_element("xpath",'//*[#id="ctl00_MenuTabNew"]/ul/li[4]/span/span')
time.sleep(5)
hover = ActionChains(driver).move_to_element(element_to_hover_over)
hover.perform()
time.sleep(5)
driver.find_element("xpath", '//*[#id="ctl00_MenuTabNew"]/ul/li[4]/div/ul/li[1]/a/span').click()
#time.sleep(5)
driver.find_element("id","ctl00_ContentPlaceHolder1_ShipOrderGrid_ctl00_ctl02_ctl00_btnAdd").click()
#time.sleep(5)
driver.find_element("class name",'RadButton RadButton_Hay rbLinkButton btn_new_bg_popUpnew').click()
#time.sleep(5)
element=driver.find_element("xpath",'//*[#id="btnSave"]').click()
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
driver = webdriver.Chrome(executable_path = r"C:\Users\mvintee\project_for_selenium\chromedriver_win32\chromedriver.exe")
actions = ActionChains(driver)
driver.get("http://pis033/NorthStarWMS/User/PurchaseOrders.aspx?t=1&PO=1")
print(driver.title)
driver.find_element("id","Login1_UserName").send_keys("wms")
driver.find_element("id","Login1_Password").send_keys("1")
driver.find_element("class name","rbText").click()
time.sleep(5)
element_to_hover_over=driver.find_element("xpath",'//*[#id="ctl00_MenuTabNew"]/ul/li[4]/span/span')
time.sleep(5)
hover = ActionChains(driver).move_to_element(element_to_hover_over)
hover.perform()
time.sleep(5)
driver.find_element("xpath", '//*[#id="ctl00_MenuTabNew"]/ul/li[4]/div/ul/li[1]/a/span').click()
#time.sleep(5)
driver.find_element("id","ctl00_ContentPlaceHolder1_ShipOrderGrid_ctl00_ctl02_ctl00_btnAdd").click()
#time.sleep(5)
driver.find_element("class name",'RadButton RadButton_Hay rbLinkButton btn_new_bg_popUpnew').click()
#time.sleep(5)
element=driver.find_element("xpath",'//*[#id="btnSave"]').click()

Scrapy-Selenium: Output contains 'None' value

I am trying to execute this script using Scrapy-Selenium middleware to handle JavaScript. This script is not showing any error but provides 'None' output. I think I am missing something from this
followers = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//a[#href='/goat/followers/']"))).click()
time.sleep(3)
particular line but I am unable to figure it out. What am I actually missing here? I am stuck at this point very badly.
import scrapy
from scrapy_selenium import SeleniumRequest
from selenium.webdriver.common.keys import Keys
from scrapy.selector import Selector
from goat.settings import *
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By import time
class FollowersSpider(scrapy.Spider):
name = 'followers'
start_urls = [
"https://www.instagram.com"
]
def __init__(self):
chrome_options = Options()
chrome_options.add_argument('__headless')
chrome_path = SELENIUM_DRIVER_EXECUTABLE_PATH
driver = webdriver.Chrome(executable_path=chrome_path, options=chrome_options)
driver.set_window_size(1920, 1080)
driver.get("https://www.instagram.com/goat/")
#target username
username = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[#name='username']")))
password = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[#name='password']")))
#enter username and password
username.clear()
username.send_keys("username")
time.sleep(2)
password.clear()
password.send_keys("password")
time.sleep(2)
#target the login button and click it
button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[type='submit']"))).click()
time.sleep(2)
#We are logged in!
#closing pop_up
pop_up = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(text(), 'Not Now')]"))).click()
time.sleep(2)
#closing notification pop_up
notification_pop_up = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(text(), 'Not Now')]"))).click()
time.sleep(2)
#search_box
searchbox = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[#placeholder='Search']")))
searchbox.clear()
keyword = "#goat"
searchbox.send_keys(keyword)
# searchbox.send_keys(keyword, Keys.ENTER)
time.sleep(2)
select_search = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//a[#href='/goat/']"))).click()
time.sleep(3)
followers = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//a[#href='/goat/followers/']"))).click()
time.sleep(3)
select_follower = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//span[#class='Jv7Aj mArmR MqpiF ']/a"))).click()
time.sleep(3)
self.html = driver.page_source
driver.quit()
def parse(self, response):
resp = Selector(text=self.html)
links = resp.xpath("//span[#class='Jv7Aj mArmR MqpiF ']/a")
for link in links:
yield {
'Title': link.xpath(".//div[#class='XBGH5']/h2/text()").get(),
'Posts': link.xpath("(.//span[#class='g47SY '])[1]/text()").get(),
'Followers': link.xpath("(.//span[#class='g47SY '])[2]/text()").get()
}

Selenium: selenium.common.exceptions.TimeoutException: Message: error

I was trying to run this script to automate and click on the product list in the webpage using selenium. But this "raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: error" is occurring every time. What is wrong I'm doing here? Expecting your guidance.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from shutil import which
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
chrome_options = Options()
chrome_options.add_argument('__headless')
chrome_path = which('chromedriver')
driver = webdriver.Chrome(executable_path=chrome_path, options=chrome_options)
driver.set_window_size(1920, 1080)
driver.get('https://www.galaxus.ch/search?q=5010533606001')
product_tab = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//article[#class='panelProduct panelLayout_panelLayout__BDQ6_ view_product__3AOqY']/a"))).click()
time.sleep(10)
driver.close()
output
PS G:\Python_Practice\scrapy_practice\test> [21628:13792:0911/125833.339:ERROR:gpu_init.cc(441)] Passthrough is not supported, GL is disabled
> & C:/Users/raisu/anaconda3/envs/Scrapy_Workspace2/python.exe g:/Python_Practice/scrapy_practice/test/test.py
DevTools listening on ws://127.0.0.1:56456/devtools/browser/1d6d20ce-ecb9-44f7-be6e-1dbe1373526a
Traceback (most recent call last):
File "g:/Python_Practice/scrapy_practice/test/test.py", line 18, in <module>
product_tab = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//article[#class='panelProduct panelLayout_panelLayout__BDQ6_ view_product__3AOqY']/a"))).click()
File "C:\Users\raisu\anaconda3\envs\Scrapy_Workspace2\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
Instead of using the xpath, I've used a CSS_SELECTOR and also I changed the wait condition for element to be visible
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from shutil import which
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
chrome_options = Options()
chrome_options.add_argument('__headless')
chrome_path = which('chromedriver')
driver = webdriver.Chrome(executable_path=chrome_path, options=chrome_options)
driver.set_window_size(1920, 1080)
driver.get('https://www.galaxus.ch/search?q=5010533606001')
#time.sleep(5) use this only if the wait is not working
wait = WebDriverWait(driver, 20)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'article > a')))
driver.find_element(By.CSS_SELECTOR,'article > a').click()
driver.close()

The website it opens using the automated software closes soon after it gets executed

Error could possibly be in line 7,
it should open the browser search for the specific word then should the window should stay until closed
import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
class PythonOrgSearch(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome("C:/Users/daniy/AppData/Local/Programs/Python/Python37-32/Scripts/chromedriver.exe")
def test_search_in_python_org(self):
driver = self.driver
driver.get("http://www.python.org")
self.assertIn("Python", driver.title)
driver.implicitly_wait(6000)
elem = driver.find_element_by_name("q")
driver.implicitly_wait(5000)
elem.send_keys("pycon")
driver.implicitly_wait(6000)
elem.send_keys(Keys.RETURN)
driver.implicitly_wait(6000)
assert "No results found." not in driver.page_source
driver.implicitly_wait(9000)
if __name__ == "__main__":
unittest.main()
Try this out it works for me.
driver.get("http://www.python.org")
self.assertIn("Python", driver.title)
elem=WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.NAME, "q")))
elem.send_keys("pycon")
elem.send_keys(Keys.RETURN)
driver.implicitly_wait(5)
self.assertNotIn("No results found.", driver.page_source)
Import
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
You should use the right version of ChromeDrive as per your browser
refer to this link: https://chromedriver.chromium.org/downloads
Try the below code it should work
import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
class PythonOrgSearch(unittest.TestCase):
def setUp(self):
self.driver = self.driver = webdriver.Chrome("C:/Users/daniy/AppData/Local/Programs/Python/Python37-32/Scripts/chromedriver.exe")
def test_search_in_python_org(self):
driver = self.driver
driver.implicitly_wait(6000)
driver.get("http://www.python.org")
self.assertIn("Python", driver.title)
time.sleep(6000)
elem = driver.find_element_by_name("q")
elem.send_keys("pycon")
elem.send_keys(Keys.RETURN)
time.sleep(6000)
assert "No results found." not in driver.page_source
if __name__ == "__main__":
unittest.main()