How click in Button with Selenium python - python

I want to click the configuration button, which leads to discord profile information,
Guys, I want to click on the gear at the bottom, how can I do this? This is my code:
option = Options()
option.add_argument("--no-sandbox")
driver = webdriver.Chrome(options=option)
driver.get("https://discord.com/channels/395582581124497408/395582581124497412")
wait = WebDriverWait(driver, 5)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'a.item'))).click()
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div#contents-18-Yxp"))).click()
Can anyone help me with this problem?

you can click it with this code:
driver.find_element_by_css_selector('div'[aria-label="User settings"]').click()

Find the ID of that button, then use driver.find_element_by_id("ID").click()
You should read this tutorial https://www.geeksforgeeks.org/click-element-method-selenium-python/

First, you must get an identifier for your element, for example:
class
xpath
css selector
... etc
then drive.find_element_by_(class, xpath, ...)(string).click()
Example:
driver.find_element_by_xpath('(//*[#class="contents-18-Yxp"])[7]')
this is the identifier for the settings section, you can copy it, note that this xpath can be changed if the discoed is updated, the solution is easy, just with the same way get the new xpath and paste it instead of the old one.
with the same Technique, find the user profile identifier, and paste it and remember .click()

Related

Selenium: How to click a label

I'm trying to click this object with selenium (pyhton) using the code:
driver.find_element_by_('publicForm_customFields').click()
But I'm receiving this error:
id="publicForm_customFields" tabindex="0" type="radio" value="value"> is not clickable at point (480, 98). Other element would receive the click: value
"Other element would receive the click" means that you have another element over your element. There are a couple of options to get around this:
Try to find another element above in the DOM tree and click on it.
Use js click, you need to write a function like this:
def js_click(self, element):
element = driver.find_element_by_id("myid")
driver.execute_script("arguments[0].click();", element)
js script will click on the element even if it is intersected by another
There might be two possibilities:
1- Your locator to find element might be wrong or not unique.
2- You need to apply explicit wait till element is ready [load successfully] to be clickable.
Hope above possibilities might help you out, Or you share the link of the sight so I might debug correctly.
You are not passing any find method to the driver. Try refactoring the code to:
driver.find_element_by_id('publicForm_customFields').click()
Also, try using some sort of wait so the driver does not click before the page/element is loaded.
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.ID, 'publicForm_customFields'))).click()

How to hover over and expand dropdown menu on komoot with selenium?

My goal is to get an element within a dropdown menu that needs to be clicked in order to appear using python selenium. Also, it seems that before the click, hovering is necessary.
I tried to click the dropdown menu like this:
element = wait().until(EC.presence_of_element_located((By.CLASS_NAME, "c-topmenu c-topmenu--create c-topmenu--userbar tw-inline-flex")))
element.click()
def wait():
return WebDriverWait(driver, 30)
And tried to use hovering:
Hover = ActionChains(driver).move_to_element(element)
Hover.click().build().perform()
Still, I get a TimeoutException and can't figure out a solution.
The website komoot looks like this before clicking the dropdown menu:
The website komoot looks like this after expanding it:
Ok here is how i was able to get this working.
after the login i grapped the wrapper element from the link with:
more_menu_wrapper = driver.find_element_by_css_selector("div[data-test-id='more_menu']")
after that i could click the desired element with:
more_menu_wrapper.find_element_by_tag_name("a").click()
You are using a wrong locator.
In case of multiple class names you should use css_selector or XPath, not by class name.
Also, here you have an unique attribute of data-test-id, you should use it.
Also, you should wait for element visibility or clickability, not just for element presence.
So your code could be:
element = wait().until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div[data-test-id='more_menu']")))

Loop until button is available Python Selenium

I'm coding my first "bot" with python and webdriver
I would like to loop until a specific button shows up on the website. My idea is to check if the button is available. If not, sleep for a minute, reload, and check again. But I dont know the right syntax as this element seems too complicated for me.
Does anyone have a tip?
Edit: When the product is sold out, this button does not exist on the product page. I guess enabled/disabled won't work here, because the button does not exist if the product is unavailable. I guess I need to check, if the button exists.
You can use WebDriverWait in python selenium to wait till the element is loaded. Or you can use javascript to check if the element exists in the DOM or not:
buttons = document.getElementsByClassName("mu-button mu-button--type_alternative add-to-cart");
if (buttons.length == 0) {
# it doesn't exist
} else
{
# do something
}
Full documentation: https://selenium-python.readthedocs.io/waits.html
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "myDynamicElement"))
)

Problems with clicking a button

I wanted the program to click the 'PLAY' Button on spotify to play a song, but sadly I get this error
selenium.common.exceptions.JavascriptException: Message: javascript error: arguments[0].click is not a function
The code I tried is
WebDriverWait(driver, 3).until(EC.presence_of_element_located((By.XPATH,"//*[text()='PLAY']")))
time.sleep(1)
driver.execute_script("arguments[0].click();", driver.find_element_by_xpath("//*[text()='PLAY']"))
Can someone help me to find the button and click it, I tried also the
driver.find_element_by_xpath("//*[text()='PLAY']").click()
But I get the error element is not interactable
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOf(driver.find_element_by_xpath("//div[#class='TrackListHeader__button
TrackListHeader__button--top']//button")));
Thread.sleep(1000);
driver.find_element_by_xpath("//div[#class='TrackListHeader__button
TrackListHeader__button--top']//button").click();
also try this one
EC has a good method for verifying element is clickable before making an action element_to_be_clickable.
Also, I found over 20 elements on Spotify using your XPath, so I modified it to look for button instead of *.
Try this:
WebDriverWait(driver, 3).until(EC.element_to_be_clickable((By.XPATH,"//button[text()='PLAY']")))
driver.find_element_by_xpath("//button[text()='PLAY']").click()
I hope this helps. Good luck!

Cannot click button - element not found

I have tried to make my script click the log in button on the Spotify login page. I have been searching all day for a solution, but no matter what class, css or whatever I put in, it is just saying it could not find the object.
My code:
import selenium
from selenium import webdriver
browser = webdriver.Firefox()
browser.get("https://accounts.spotify.com/en/login?
continue=https:%2F%2Fwww.spotify.com%2Fdk%2Faccount%2Foverview%2F")
username = browser.find_element_by_id("login-username")
password = browser.find_element_by_id("login-password")
username.send_keys("myUser")
password.send_keys("myPass")
login = browser.find_element_by_class_name('ng-pristine ng-valid-sp-
disallow-chars ng-invalid ng-invalid-required').click()
It is the last line I have a problem with. Note: This is just one of the options I tried.
You can try this code that have xpath:
login = browser.find_element_by_xpath("//button[contains(.,'Log In')]");
login.click();
You're using multiple classes in your class selector. Try using only one class. Something like this:
browser.find_element_by_class_name('btn-green').click()
If it still doesn't work, perhaps the element has not yet been rendered when you try to click it, so you may want to try with an explicit wait
Also, I checked out your selector and it looks like you weren't even pointing to the button, but to the form.
And finally, a class selector may not be the optimal way to locate your element here. I'd probably go with a XPath selector to find the button that contains the text Log In, using the following XPath
//button[text()='Log In']
Use this, i Updated the answer:
login = browser.find_element_by_xapth("//button[#class='btn btn-sm btn-block btn-green ng-binding']")
login .click()
browser.find_element_by_xpath(".//button[contains(text(), 'Log In'])").click()
This should work so give it a try. :)
As #Verv stated, you could be having timing issues. So just for the test try with time.sleep() to see if this is it.

Categories

Resources