Python Selenium Value select - python

Can someone help me please, I have been stuck in one place in my script for 2 days .
A menu should be clicked and an option should be selected, unfortunately it does not work, I have started about 50 code attempts, unfortunately without success
This is the html text for which I need a Selenium code solution
<select class="categoryAttributesSelect form-control"><option value="-1">Bitte Zustand auswählen...</option><option value="22">Neu</option><option value="2546">Neuwertig</option><option value="23">Gebraucht</option><option value="24">Defekt</option></select>
I need a code that selects the option (new) for me
This not Working..
minimenu1 = Select(browser.find_element_by_xpath('/html/body/div[4]/div/div/form/div[1]/div[1]/div[2]/div[2]/div[3]/div[2]/div[4]/div[1]/div[2]/select'))
minimenu1.select_by_visible_text("Neu")
minimenu1.select_by_value("22")

See if this works.
minimenu1 = Select(browser.find_element_by_xpath("//select[contains(#class,'categoryAttributesSelect')]"))
minimenu1.select_by_visible_text("Neu")

You can use the below code :
wait = WebDriverWait(driver, 10)
menu = Select(wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "select.categoryAttributesSelect.form-control"))))
menu.select_by_visible_text('Neu')
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

NoSuchElementException when using Xpath

So I am trying to access the website and I want the program to apply some filters for me via click. Although when I try to use the function find_element_by_xpath it says that this function is depreciated. Can someone help me ? Also the website is kind of weird because it is more like a dashboard than a website and so I dont know how precise xpath will work. thank you all so much in advance
The Solution For this problem is :
using the new feature find_element()
include this import too
from selenium.webdriver.common.by import By
this is an example :
button = driver.find_element_by_class_name("quiz_button")
button = driver.find_element(By.CLASS_NAME, "quiz_button")
please see this selenium documentation : https://www.selenium.dev/selenium/docs/api/py/api.html
driver.find_element_by_xpath('//*[#id="filtro-04"]/div/article/div[1]/div/div/qv-
filterpane/div/div/div/div[2]/span').click()
Due to Depreciation Warning use the updated By method
driver.find_element(By.XPATH,'//*[#id="filtro-04"]/div/article/div[1]/div/div/qv-
filterpane/div/div/div/div[2]/span').click()
From there due to page loading.
wait = WebDriverWait(driver, 30)
wait.until(EC.element_to_be_clickable((By.XPATH, '//*[#id="filtro-04"]/div/article/div[1]/div/div/qv-filterpane/div/div/div/div[2]/span'))).click()
Imports:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

Selenium Python Click function doesn't work

I'm having a big problem with this one and I just don't know what to do now.
So, I'm trying to use selenium to get an information on a sort of a pop up.pop up
(This is this exact pop-up, it's on tiktok)
The HTML element of the button followers : Followers<
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
driver = webdriver.Firefox(executable_path='./geckodriver')
driver.get("https://www.tiktok.com/#programm___r?lang=en") # on lance tiktok sur ordi
# first thing first, just click on the button that "launch" the pop up
driver.find_element_by_class_name('jsx-1737374796.header-inbox-icon').click()
# Then, I want to click on "Followers" category but this is getting complicated here
# First idea to click on the category, check if it contains the word "followers"
if (driver.find_element_by_xpath('/html/body/div[1]/div/div[1]/div/div[3]/div[2]/div[2]/div/div[1]/div/span[5]').text) == "Followers" : # this line works, no problem
driver.find_element_by_xpath('/html/body/div[1]/div/div[1]/div/div[3]/div[2]/div[2]/div/div[1]/div/span[5]').click() # this one doesn't work i don't know why
# So, second idea, try with the webdriverwait
WebDriverWait(driver,20).until(EC.presence_of_element_located((By.XPATH,'/html/body/div[1]/div/div[1]/div/div[3]/div[2]/div[2]/div/div[1]/div/span[5]'))) # this works
driver.find_element_by_xpath('/html/body/div[1]/div/div[1]/div/div[3]/div[2]/div[2]/div/div[1]/div/span[5]').click() # this still doesn't work
# Third idea, instead o xpath, css-selector
WebDriverWait(driver,20).until(EC.presence_of_element_located((By.CSS_SELECTOR,"span.jsx-2344352055:nth-child(5)"))) # work, no problem
driver.find_element_by_css_selector("span.jsx-2344352055:nth-child(5)").click() # doesn't work neither..
# Fourth and last idea, probably the least fine, get all the elements with the class name, and only one return Followers with the .text, but still the same problem
elements = (driver.find_elements_by_class_name("jsx-2344352055"))
for i in range(len(elements)) :
if elements[i].text == "Followers" :
elements[i].click() # but still doesn't work
Sometimes, it worked, like I don't know why or how but sometimes, the click works, but like 95% of the time, it doesn't and I really don't know why
Thanks in advance for your help !
You should wait for the button to be clickable using WebDriverWait and element_to_be_clickable expected condition. For example:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
driver = webdriver.Firefox(executable_path='./geckodriver')
driver.get("https://www.tiktok.com/#programm___r?lang=en")
WebDriverWait(driver,20).until(EC.presence_of_element_located((By.XPATH,'/html/body/div[1]/div/div[1]/div/div[3]/div[2]/div[2]/div/div[1]/div/span[5]')))
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,'/html/body/div[1]/div/div[1]/div/div[3]/div[2]/div[2]/div/div[1]/div/span[5]'))).click()
Please try this one:
driver = webdriver.Firefox(executable_path='./geckodriver')
driver.get("https://www.tiktok.com/#programm___r?lang=en") # on lance tiktok sur ordi
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS) ;
# first thing first, just click on the button that "launch" the pop up
driver.find_element_by_class_name('jsx-1737374796.header-inbox-icon').click()
I hope this resolves your issue.

Why can't Xpath click the element?

I am Trying to create a bot that can fill a cart with these beer bottles. I really want to do this on a few different sites but for some reason i can only get it to open the page and click the first button, then it doesn't click the next button. I tried ID, Name, pretty much anyway to identify the button but it won't click it. I even tried sleep for 3 seconds. I tried to see if it was in an Iframe but i don't think it is. I'm out of ideas.... Link is https://www.sideprojectbrewing.com/shop?category=Beer+Release
I'm trying to access the add to cart element but does not seem to work
\
from Config import keys
from selenium import webdriver
def order(k):
driver = webdriver.Chrome(executable_path=r"C:\Users\ezliv\Desktop\ShopBot1\chromedriver_win32\chromedriver.exe")
driver.get(k['product_url'])
driver.find_element_by_xpath('//*[#id="thumb-biereblanche"]/div/div[1]/div/div/img').click()
driver.find_element_by_xpath('//*[#id="yui_3_17_2_1_1606181545139_755"]').click()
\\
You can try following code:
driver.get(url_here)
wait = WebDriverWait(driver, 20)
bottle = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[#id="thumb-biereblanche"]/div/div[1]/div/div/img')))
bottle.click()
add_to_cart = wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'sqs-add-to-cart-button-inner')))
add_to_cart.click()
Import:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

Press button with Selenium in Python

Path here
I need to press the button but cant really figure out how
I tried copying the xpath but get an error
also tried to press the button over the id or class_name but also not worked
If you have an idea please write it in the comments. Thanks
Tried so far:
driver.find_element_by_xpath("//button[#class='action-button']").click()
driver.find_element_by_class_name("action-button").click()
driver.find_element_by_xpath("[#id='clearBrowsingDataConfirm']").click()
driver.find_element_by_id('[#id="clearBrowsingDataConfirm"]').click()
Should work if it's not in a iframe.
driver.find_element_by_id('clearBrowsingDataConfirm').click()
Try this if it's after driver.get()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, 'clearBrowsingDataConfirm'))).click()
Import
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

Python + Selenium -- Unable to locate image element (Can't click image)

I'm trying to create a macro that opens up all my online classes on Chrome (cause logging in is annoying, especially if you have to do it 8 times every morning).
BTW, I use Python 3.8, Chrome 81.0.4044.122, and the latest version of Selenium.
Until now, I clicked button using:
driver = webdriver.Chrome()
driver.find_element_by_xpath("PATH_OF_ELEMENT").click()
And then I find a login button that has a image instead of text.
I tried XPath, CSS Selector, id, name, the link of text, ActionChains (move_to) nothing works.
Here's the HTML:
here click me please.
The button I'm trying to press is the one with the tag a.
I spent 30 minutes googling about this and all I found was Stack Overflow questions from 6 years ago. They suggested I use WebDriverWait or change the frame. None of them worked (I might have made a mistake). I'm new to Selenium so please be kind and explain hard stuff.
How can I find the correct XPath of an image button and click it?
driver.find_element_by_css_selector('.nice-select').click()
driver.find_element_by_xpath("/html/body/div1/div[3]/div/div/div/div/div/div2/div1/div/ul/li2").click()
driver.find_element_by_xpath('/html/body/div1/div[3]/div/div/div/div/div/div2/div2/div/span').click()
driver.find_element_by_xpath('/html/body/div1/div[3]/div/div/div/div/div/div2/div2/div/ul/li[19]').click()
driver.find_element_by_xpath('/html/body/div1/div[3]/div/div/div/div/div/div2/div[3]/div/span').click()
driver.find_element_by_xpath('/html/body/div1/div[3]/div/div/div/div/div/div2/div[3]/div/ul/li[3]').click()
driver.find_element_by_xpath('/html/body/div1/div[3]/div/div/div/div/div/div2/div[4]/div/span').click()
driver.find_element_by_xpath('/html/body/div1/div[3]/div/div/div/div/div/div2/div[4]/div/ul/li[3]').click()
driver.find_element_by_xpath('/html/body/div1/div[3]/div/div/div/div/div/div2/div[5]/a').click()
Try the following CSS Selector:
.my_menu>a
Code should look like:
driver.find_element_by_css_selector(".my_menu>a").click()
Also, try to locate the element with explicit wait:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".my_menu>a"))).click()
I have tested with the following code block (as result will be displayed popup with 2 options):
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import time
driver = webdriver.Chrome()
driver.get("https://oc31.ebssw.kr/onlineClass/search/onlineClassSearchView.do?schulCcode=00898&schCssTyp=online_mid")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable(
(By.CSS_SELECTOR, ".my_menu>a"))).click()
time.sleep(5)
I hope it helps you!

Categories