Problems with clicking a button - python

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!

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()

Button/Element not interactable

I am trying to click on a button in a webpage using Selenium and xpath. I managed to click on a button in the previous page but after loading into this new page, I tried to use the same code but different button HTML, it was not able to load as before.
My code is as follows:
driver = webdriver.Chrome()
driver.get("https://connect.com")
driver.find_element_by_xpath("//button[#class='p-button p-component']").click() #button workable
In the next page:
driver.find_element_by_xpath("//button[#class='add icon-only proto-button ng-star-inserted']").click() #button not working
The error message that I have received is:
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
I have also tried with adding in waiting time as below, but it is still not working.
button = WebDriverWait(driver, 10).until(ec.element_to_be_clickable((By.XPATH, "//button[#class='add icon-only proto-button ng-star-inserted']")))
button.click()
However, now the error message that I have received is:
selenium.common.exceptions.TimeoutException: Message:
Alternatively, I have tried to work on other elements of the HTML code, for example. But either way, the button did not work.
driver.find_element_by_xpath("//span[text()='proto-icon ng-star-inserted']").click()
or
driver.find_element_by_xpath("//connect-button[#class='proto-button-list-item ng-star-inserted']").click()
The HTML is in the picture attached
I have no idea how else can i try to work around. Please advise, thanks !
I agree with #lucasnguyen17
The button element must be not clickable because of the connect-button element over it.
So
element = driver.find_element_by_xpath("//button[#class='p-button p-component']")
driver.execute_script("arguments[0].click();", element)
It's really simple,
In the first line it stores the element as a python variable.
Then it runs the js script.
element.click();
Where the element is your specific button. You can try it on the console to test if it works.
A link to the specific page would have been helpful.

How click in Button with Selenium 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()

How do I click on a navigation bar item using Selenium?

I am fairly new in Selenium and I've been trying to work on automating the login for this [website], but for some reason element.click() on selenium does not seem to work when I try to click onto the Login button. I keep getting this TypeError: 'str' object is not callable error.
Here's my code:
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH("//li[#class='item-119']/a[contains(text(),'Login')]")))
)
print("TEST")
element.click()
finally:
driver.quit()
I've also tried using By.CLASS_NAME and By.PARTIAL_LINK_TEXT and I keep getting the same error. Spent a few hours researching, looking through StackOverFlow and trying to solve this error but I don't seem to be able to solve it. Please do help me and let me know where I went wrong.
You are trying to use wrong locator.
Selenium fails to find such element.
Also, it's better to use expected conditions of element visibility instead of element presence since when element becomes existing on the page it is still not clickable / visible.
So, please try this:
element = wait.until(EC.visibility_of_element_located((By.XPATH, "//div[#id='header']//a[#class='login-btn']")))
print("TEST")
element.click()
You can try with below xpath :
try:
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH("(//a[#class='login-btn'])[2]"))).click()
print("TEST")
finally:
driver.quit()

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"))
)

Categories

Resources