So here's my code first:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
import time
from fake_useragent import UserAgent
import random
ua = UserAgent()
options = Options()
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--blink-settings=imagesEnabled=false')
chrome_options.add_argument('--headless')
chrome_options.add_argument(f'user-agent={ua.random}')
driver = webdriver.Chrome(options=options, chrome_options=chrome_options)
driver.maximize_window()
url = "https://magiceden.io/marketplace/hasuki"
driver.get(url)
element = driver.find_element(By.CSS_SELECTOR, "#content > div.tw-w-full.tw-py-0.sm\:tw-mt-0 > div.tw-flex.tw-relative > div.tw-flex-auto.tw-max-w-full.tw-pt-0 > div.tw-flex.tw-items-center.md\:tw-justify-between.tw-gap-2.md\:tw-gap-4.md\:tw-sticky.tw-top-\[133px\].tw-bg-gray-100.tw-z-10.tw-flex-wrap.tw-p-5 > div.tw-flex.tw-flex-grow.tw-justify-center.tw-gap-x-2 > button > span:nth-child(4)")
print(f"The current instant sell price is {element.text}")
When I run it, I get weird long error, that ends with:
Backtrace:
(No symbol) [0x00806643]
(No symbol) [0x0079BE21]
(No symbol) [0x0069DA9D]
(No symbol) [0x006D1342]
(No symbol) [0x006D147B]
(No symbol) [0x00708DC2]
(No symbol) [0x006EFDC4]
(No symbol) [0x00706B09]
(No symbol) [0x006EFB76]
(No symbol) [0x006C49C1]
(No symbol) [0x006C5E5D]
GetHandleVerifier [0x00A7A142+2497106]
GetHandleVerifier [0x00AA85D3+2686691]
GetHandleVerifier [0x00AABB9C+2700460]
GetHandleVerifier [0x008B3B10+635936]
(No symbol) [0x007A4A1F]
(No symbol) [0x007AA418]
(No symbol) [0x007AA505]
(No symbol) [0x007B508B]
BaseThreadInitThunk [0x75EB00F9+25]
RtlGetAppContainerNamedObjectPath [0x77A27BBE+286]
RtlGetAppContainerNamedObjectPath [0x77A27B8E+238]
BUT if I comment out "chrome_options.add_argument('--headless')", my code works perfectly fine. What's the issue here? I suppose the problem is that website doesn't let me use headless mode, how can I solve this?
I want my program to run in headless mode, but I get restricted either by the website or chrome browser.
Basically, you are seeing a NoSuchElementException
Full stacktrace:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"#content > div.tw-w-full.tw-py-0.sm\:tw-mt-0 > div.tw-flex.tw-relative > div.tw-flex-auto.tw-max-w-full.tw-pt-0 > div.tw-flex.tw-items-center.md\:tw-justify-between.tw-gap-2.md\:tw-gap-4.md\:tw-sticky.tw-top-\[133px\].tw-bg-gray-100.tw-z-10.tw-flex-wrap.tw-p-5 > div.tw-flex.tw-flex-grow.tw-justify-center.tw-gap-x-2 > button > span:nth-child(4)"}
(Session info: headless chrome=109.0.5414.75)
Stacktrace:
Backtrace:
(No symbol) [0x00E04AD3]
(No symbol) [0x00D99211]
(No symbol) [0x00C8D9CD]
(No symbol) [0x00CC1102]
(No symbol) [0x00CC123B]
(No symbol) [0x00CF8A72]
(No symbol) [0x00CDFB84]
(No symbol) [0x00CF67B9]
(No symbol) [0x00CDF936]
(No symbol) [0x00CB4788]
(No symbol) [0x00CB5C1D]
GetHandleVerifier [0x01079342+2502274]
GetHandleVerifier [0x010A7959+2692249]
GetHandleVerifier [0x010AABDC+2705180]
GetHandleVerifier [0x00EB3480+643008]
(No symbol) [0x00DA208F]
(No symbol) [0x00DA7AB8]
(No symbol) [0x00DA7BA5]
(No symbol) [0x00DB273B]
BaseThreadInitThunk [0x76D9FA29+25]
RtlGetAppContainerNamedObjectPath [0x77B37A4E+286]
RtlGetAppContainerNamedObjectPath [0x77B37A1E+238]
Solution
The desired element is a dynamic element, so ideally to print the desired text from the element you need to induce WebDriverWait for the visibility_of_element_located() and you can use the following Locator Strategy:
Using CSS_SELECTOR:
driver.get("https://magiceden.io/marketplace/hasuki")
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "#content > div.tw-w-full.tw-py-0.sm\:tw-mt-0 > div.tw-flex.tw-relative > div.tw-flex-auto.tw-max-w-full.tw-pt-0 > div.tw-flex.tw-items-center.md\:tw-justify-between.tw-gap-2.md\:tw-gap-4.md\:tw-sticky.tw-top-\[133px\].tw-bg-gray-100.tw-z-10.tw-flex-wrap.tw-p-5 > div.tw-flex.tw-flex-grow.tw-justify-center.tw-gap-x-2 > button > span:nth-child(4)"))).text)
driver.quit()
Console Output:
0.3664
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Related
Hello stackoverflow comunity, i'm facing a problem with .find element of selenium. I'm programing in python and using google chrome.
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time
chrome_driver_path = "C:\Development\chromedriver.exe"
driver = webdriver.Chrome(service=Service(chrome_driver_path))
driver.get("https://tinder.com/")
time.sleep(5)
accept = driver.find_element(By.XPATH, value='//*[#id="c-1351236777"]/div/div[1]/div/div/main/div/div[2]/div/div[3]/div/div/button[2]')
accept.click()
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: 'value' must be a string
(Session info: chrome=110.0.5481.104)
Stacktrace:
Backtrace:
(No symbol) [0x002B6643]
(No symbol) [0x0024BE21]
(No symbol) [0x0014DA9D]
(No symbol) [0x001813F3]
(No symbol) [0x0018147B]
(No symbol) [0x001B8DFC]
(No symbol) [0x0019FDC4]
(No symbol) [0x001B6B09]
(No symbol) [0x0019FB76]
(No symbol) [0x001749C1]
(No symbol) [0x00175E5D]
GetHandleVerifier [0x0052A142+2497106]
GetHandleVerifier [0x005585D3+2686691]
GetHandleVerifier [0x0055BB9C+2700460]
GetHandleVerifier [0x00363B10+635936]
(No symbol) [0x00254A1F]
(No symbol) [0x0025A418]
(No symbol) [0x0025A505]
(No symbol) [0x0026508B]
BaseThreadInitThunk [0x762600F9+25]
RtlGetAppContainerNamedObjectPath [0x77167BBE+286]
RtlGetAppContainerNamedObjectPath [0x77167B8E+238]
(No symbol) [0x00000000]
this is the code and it doesn't run. And i think it's because the body tag of the html code is closed, I need to open it with selenium. I actually can open it if i do right click in when i inspect the code manually. Then I click in "expand recursively", then the code runs well, but i don't know how to do it automatically with python.
The I accept element is a dynamic element, so to click on the clickable element you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following locator strategy:
Using XPATH:
driver.get('https://tinder.com/')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button//div[text()='I accept']"))).click()
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
I'm trying to open chrome with default browser by Selenium. Once the chrome is opened, the cell output shows an error:
"WebDriverException"
"Message: unknown error: Chrome failed to start: exited normally.\n (unknown error: DevToolsActivePort file doesn't exist)\n (The process started from chrome location C:\\\\Program Files\\\\Google\\\\Chrome\\\\Application\\\\chrome.exe is no longer running, so ChromeDriver is assuming that Chrome has crashed.)\nStacktrace:\nBacktrace:\n\t(No symbol) [0x00B0F243]\n\t(No symbol) [0x00A97FD1]\n\t(No symbol) [0x0098D04D]\n\t(No symbol) [0x009AC24E]\n\t(No symbol) [0x009A82E9]\n\t(No symbol) [0x009DF056]\n\t(No symbol) [0x009DEB2A]\n\t(No symbol) [0x009D8386]\n\t(No symbol) [0x009B163C]\n\t(No symbol) [0x009B269D]\n\tGetHandleVerifier [0x00DA9A22+2655074]\n\tGetHandleVerifier [0x00D9CA24+2601828]\n\tGetHandleVerifier [0x00BB8C0A+619850]\n\tGetHandleVerifier [0x00BB7830+614768]\n\t(No symbol) [0x00AA05FC]\n\t(No symbol) [0x00AA5968]\n\t(No symbol) [0x00AA5A55]\n\t(No symbol) [0x00AB051B]\n\tBaseThreadInitThunk [0x7579FEF9+25]\n\tRtlGetAppContainerNamedObjectPath [0x77297BBE+286]\n\tRtlGetAppContainerNamedObjectPath [0x77297B8E+238]\n"
I also run without profile argument, it works fine. But when profile argument is added, the error occurs.
Chrome version: 108.0.5359.125
Python version: 3.9.12
Selenium version: 4.7.2
Code:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
chrome_options = Options()
chrome_options.add_argument("--user-data-dir=C:\\Users\\Rattapong.Pojpatin\\AppData\\Local\\Google\\Chrome\\User Data\\") # change to profile path
chrome_options.add_argument('--profile-directory=Profile 2')
browser = webdriver.Chrome(executable_path="D:\\Chromedriver\\chromedriver.exe", chrome_options=chrome_options)
browser.get('https://sellercenter.lazada.co.th/apps/seller/login')
i'm trying to make a program that as the first step must login on site edostavka.by having phone and password. If I use "https://edostavka.by/login?screen=withPassword" - program still redirected on "https://edostavka.by/login?screen=phone". Luckily there is button "Войти с паролем", which should open "https://edostavka.by/login?screen=withPassword" page. But selenium can't find it.
My code here:
from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from fake_useragent import UserAgent as ua
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
ua = ua(browsers='chrome')
driver_service = Service(executable_path=r'C:\Users\37533\Desktop\dostavka\dostavka\chromedriver\chromedriver.exe')
options = webdriver.ChromeOptions()
options.add_argument(f'user-agent={ua}')
driver = webdriver.Chrome(service=driver_service)
driver.maximize_window()
#url = 'https://edostavka.by/login?screen=withPassword'
url = 'https://edostavka.by/login?screen=phone'
try:
driver.get(url=url)
time.sleep(3)
driver.find_element(By.NAME, 'phone').send_keys('132456789')
driver.find_element(By.LINK_TEXT, 'Войти с паролем').send_keys(Keys.ENTER)
time.sleep(5)
except Exception as e:
print(e)
finally:
driver.close()
driver.quit()
It was no matter how i tried to find that button(through XPATH, CLASS_NAME, or LINK_TEXT) - none of this worked. I tried put WebDriverWait and EC instead of time.sleep, but it didn't help aswell as swapping "click" methods(I tried 'click()', 'submit()', 'send_keys(Keys.ENTER or \n).
Also, I figure that problem might be in iframe or scripts. But for iframe - there are 4 on the page and all of them seems to be empty. For scripts - I still didn't find answer: what and should I do?
Had to mention that it can find and even fill line with "phone number"
Every time i run the code i get that message
Message: no such element: Unable to locate element: {"method":"link text","selector":"Войти с паролем"}
(Session info: chrome=108.0.5359.125)
Stacktrace:
Backtrace:
(No symbol) [0x00FEF243]
(No symbol) [0x00F77FD1]
(No symbol) [0x00E6D04D]
(No symbol) [0x00E9C0B0]
(No symbol) [0x00E9C22B]
(No symbol) [0x00ECE612]
(No symbol) [0x00EB85D4]
(No symbol) [0x00ECC9EB]
(No symbol) [0x00EB8386]
(No symbol) [0x00E9163C]
(No symbol) [0x00E9269D]
GetHandleVerifier [0x01289A22+2655074]
GetHandleVerifier [0x0127CA24+2601828]
GetHandleVerifier [0x01098C0A+619850]
GetHandleVerifier [0x01097830+614768]
(No symbol) [0x00F805FC]
(No symbol) [0x00F85968]
(No symbol) [0x00F85A55]
(No symbol) [0x00F9051B]
BaseThreadInitThunk [0x771800F9+25]
RtlGetAppContainerNamedObjectPath [0x779C7BBE+286]
RtlGetAppContainerNamedObjectPath [0x779C7B8E+238]
Maybe my question easy and newbie, but i tried to fix this problem over six-seven hours and still didn't find the way of solving
First of read the selenium documentation. Then use Chrome Developer tools (you may call by pressing F12 or Ctrl+Shift+I) to inspect target page elements.
Below I provide an example solution for your case:
# accept coockies
driver.find_element(By.XPATH, "//button[#class='button button_size_medium button_type_subtle accept-cookies_accept__mok-Y']").click()
time.sleep(2)
# change login mode to with password
driver.find_element(By.XPATH, "//button[#class='button button_size_large button_type_subtle phone_button__2Z9fs phone_button__padding__e0VFd']").click()
time.sleep(2)
# type phone number
driver.find_element(By.XPATH, "//input[#class='index-module_wrapperPhone__input__KD5gF']").send_keys('132456789')
time.sleep(2)
# type password
driver.find_element(By.XPATH, "//input[#class='input__input input__input_size_medium']").send_keys('Password')
time.sleep(2)
# click to eye icon
driver.find_element(By.XPATH, "//button[#class='withPassword_login__form_password_button__3Zrhk']").click()
time.sleep(2)
This is how to use XPath
Very new to coding and started learning selenium. Below is the code I ran which opened the browser but the moment I try to find an element within the page, I get this InvalidArgumentException error with a bunch of backtrace information. I can not post images yet since I am new so I can not show exactly what the error looks like.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys # gives access to keys like enter
import time
path = r"C:\...\chromedriver.exe"
driver = webdriver.Chrome(path)
driver.get('https://www.yahoo.com')
search = driver.find_element("_yb_nuzqf")
search.send_keys("test")
search.send_keys(Keys.RETURN)
time.sleep(5)
driver.quit()
This is the errors output:
Exception has occurred: InvalidArgumentException
Message: invalid argument: invalid locator
(Session info: chrome=108.0.5359.125)
Stacktrace:
Backtrace:
(No symbol) [0x00A3F243]
(No symbol) [0x009C7FD1]
(No symbol) [0x008BD04D]
(No symbol) [0x008EC1BE]
(No symbol) [0x008EC22B]
(No symbol) [0x0091E612]
(No symbol) [0x009085D4]
(No symbol) [0x0091C9EB]
(No symbol) [0x00908386]
(No symbol) [0x008E163C]
(No symbol) [0x008E269D]
GetHandleVerifier [0x00CD9A22+2655074]
GetHandleVerifier [0x00CCCA24+2601828]
GetHandleVerifier [0x00AE8C0A+619850]
GetHandleVerifier [0x00AE7830+614768]
(No symbol) [0x009D05FC]
(No symbol) [0x009D5968]
(No symbol) [0x009D5A55]
(No symbol) [0x009E051B]
BaseThreadInitThunk [0x77617D69+25]
RtlInitializeExceptionChain [0x77C5BB9B+107]
RtlClearBits [0x77C5BB1F+191]
I tried looking up what the error was or if anyone else had this issue. I can not seem to locate how to solve this problem
driver.find_element("_yb_nuzqf") line is wrong.
find_element method receives a By object which is a pair of "what you locating the element by" and the actual value. For example it could be
driver.find_element(By.ID, "_yb_nuzqf")
or
driver.find_element(By.CLASS_NAME, "_yb_nuzqf")
To use it you need to import this:
from selenium.webdriver.common.by import By
Use the locater to find elements.
EX >> driver.find_element(By.ID,"_yb_nuzqf")
I'm trying to code a bot that will automate a login on a certain page using selenium. I keep getting the same error, and I don't know how to fix it. Please help :=)
Here's the code:
# Importing everything #
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time
# PATH + Driver Setup #
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
# Getting All Requirements #
driver.get("https://ytmonster.net/login")
time.sleep(5)
imputUsername = driver.find_element(By.ID, "inputUsername")
imputPassword = driver.find_element(By.ID, "inputPassword")
linkClick = driver.find_element(By.CLASS_NAME, "btn btn-success")
# Executing script #
imputUsername.send_keys("mymail#gmail.com")
imputPassword.send_keys("mypassword")
linkClick.click
And here is the error that I get:
C:\Users\Xera\Desktop\YtMonster Bot.py:9: DeprecationWarning: executable_path has been deprecated, please pass in a Service object
driver = webdriver.Chrome(PATH)
DevTools listening on ws://127.0.0.1:51847/devtools/browser/a848e7ed-d31d-4415-96c5-1a28a4729a17
[8664:1220:0815/155348.366:ERROR:device_event_log_impl.cc(214)] [15:53:48.364] Bluetooth: bluetooth_adapter_winrt.cc:1074 Getting Default Adapter failed.
Traceback (most recent call last):
File "C:\Users\Xera\Desktop\YtMonster Bot.py", line 16, in <module>
linkClick = driver.find_element(By.CLASS_NAME, "btn btn-success")
File "C:\Users\Xera\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 856, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "C:\Users\Xera\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 434, in execute
self.error_handler.check_response(response)
File "C:\Users\Xera\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 243, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".btn btn-success"}
(Session info: chrome=104.0.5112.81)
Stacktrace:
Backtrace:
Ordinal0 [0x00CA78B3+2193587]
Ordinal0 [0x00C40681+1771137]
Ordinal0 [0x00B541A8+803240]
Ordinal0 [0x00B824A0+992416]
Ordinal0 [0x00B8273B+993083]
Ordinal0 [0x00BAF7C2+1177538]
Ordinal0 [0x00B9D7F4+1103860]
Ordinal0 [0x00BADAE2+1170146]
Ordinal0 [0x00B9D5C6+1103302]
Ordinal0 [0x00B777E0+948192]
Ordinal0 [0x00B786E6+952038]
GetHandleVerifier [0x00F50CB2+2738370]
GetHandleVerifier [0x00F421B8+2678216]
GetHandleVerifier [0x00D317AA+512954]
GetHandleVerifier [0x00D30856+509030]
Ordinal0 [0x00C4743B+1799227]
Ordinal0 [0x00C4BB68+1817448]
Ordinal0 [0x00C4BC55+1817685]
Ordinal0 [0x00C55230+1856048]
BaseThreadInitThunk [0x766A6739+25]
RtlGetFullPathName_UEx [0x779F90AF+1215]
RtlGetFullPathName_UEx [0x779F907D+1165]
How can I fix this? I tried everything but didn't find any answers... Thanks again for the help! :)
You can't pass multiple classnames as argument using By.CLASS_NAME as:
find_element(By.CLASS_NAME, "classname1 classname2")
Solution
To automate the login process on the webpage you can use locator strategies:
Using CSS_SELECTOR:
driver.execute("get", {'url': 'https://ytmonster.net/login'})
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#inputUsername"))).send_keys("Xera#stackoverflow.com")
driver.find_element(By.CSS_SELECTOR, "input#inputPassword").send_keys("XeraXera")
driver.find_element(By.CSS_SELECTOR, "button.btn.btn-success").click()
Using XPATH:
driver.execute("get", {'url': 'https://ytmonster.net/login'})
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#id='inputUsername']"))).send_keys("Xera#stackoverflow.com")
driver.find_element(By.XPATH, "//input[#id='inputPassword']").send_keys("XeraXera")
driver.find_element(By.XPATH, "//button[#class='btn btn-success']").click()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Browser Snapshot:
This is because your page elements get changed after you manipulate it using Selenium. Try other ways to access the desired element such as xpath. Selecting by class often causes this exception as the class of your html elements can change. For example, once a tab is clicked, the class can change to something like: tab-button active, while prior to the clicking, it was something like: tab-button.
I recommend using xpath. It is very powerful as it enhances your ability to access items in a relative way from each other. You can for example say I want the i-th children of my first row inside a table inside body inside html tag.
This is one way of accomplishing your task:
from selenium import webdriver
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.options import Options as Firefox_Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support import expected_conditions as EC
import time as t
firefox_options = Firefox_Options()
# firefox_options.add_argument("--width=1500")
# firefox_options.add_argument("--height=500")
# firefox_options.headless = True
driverService = Service('chromedriver/geckodriver')
browser = webdriver.Firefox(service=driverService, options=firefox_options)
url = 'https://ytmonster.net/login'
browser.get(url)
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#id='inputUsername']"))).send_keys('my_great_username')
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#id='inputPassword']"))).send_keys('my really bad password')
print('wrote in my user and pass')
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Login']"))).click()
print('clicked the login button!')
The setup is Firefox /geckodriver on Linux, however you can adapt it to your own system, just observe the imports, and the part after defining the browser/driver.
Selenium docs: https://www.selenium.dev/documentation/