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

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

Related

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 can I crawling on this page? I have a specific error

Question: I have the error (see in image below). I wanted to move on next page to
click button but code doesn't move on. How can I use this code?
This line is causing the error. It might be because of a wrong xpath.
driver.find_element_by_xpath("//a[#class='next']").click()
It might be easier to find the "next" element using an id or class like this-
driver.find_element_by_id("#element_id")
driver.find_element_by_css_selector("div .... a.next")
Also, the full xpath is something like this. Maybe try this once
//*[#id="react-root"]/div/div[2]/div/div[3]/section[4]/div[2]/a[12]
Invoking driver.execute seems to work due to overlapping element.
wait = WebDriverWait(driver,10)
for i in range(1, pageNum):
elem=wait.until(EC.presence_of_element_located((By.XPATH,"//a[#class='next']")))
driver.execute_script("arguments[0].click();", elem)
time.sleep(1)

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!

StaleElementReferenceException while trying to move_to_element (Python)

I'm trying to .click() a few elements in a pop up-list on a webpage, but keep getting the StaleElementReferenceException when i try to move_to_elements.
The code is based around a number of clickable elements in a feed. When clicked, these elements result in a pop-up box with more clickable elements that I want to access.
I access the pop up-box with the following code, where popupbox_links are a list with coordinates and links for the pop up boxes:
for coordinate in popupbox_links:
actions = ActionChains(driver)
actions.move_to_element(coordinate["Popupbox location"]).perform()
time.sleep(3)
popupboxpath = coordinate["Popupbox link"]
popupboxpath.click()
time.sleep(3)
This works fine. But when the pop up box is opened, I want to perform the following:
seemore = driver.find_element_by_link_text("See More")
time.sleep(2)
actions.move_to_element(seemore).perform()
time.sleep(2)
seemore.click()
time.sleep(3)
findbuttons = driver.find_elements_by_link_text("Button")
time.sleep(2)
print(findbutton)
for button in findbuttons:
time.sleep(2)
actions.move_to_element(button).perform()
time.sleep(2)
button.click()
time.sleep(randint(1, 5))
The trouble starts at actions.move_to_element on both "See more" and "Button". Even though the print(findbutton) actually returns a list with contents, containing the elements that I want to click, Selenium seems to be unable to move_to_element on these. Instead, it throws StaleElementReferenceException.
To make it more confusing, the script seems to work at times. Although usually it just crashes.
Any clues on how to solve this? Big thanks in advance.
I'm running the latest Selenium on Python 3.6 with the Chrome WebDriver.
StaleElementReferenceException Says that element is stage because something has changed in page after you created the webElement object. In you case that could be happening due to button.click().
The simplest solution is to create new element every time, rather than iterate element from the loop.
following changes might work.
findbuttons = driver.find_elements_by_link_text("Button")
time.sleep(2)
print(findbuttons)
for i in range(len(findbuttons)):
time.sleep(2)
elem = driver.find_elements_by_link_text("Button")[i]
actions.move_to_element(elem).perform()
time.sleep(2)
elem.click()
time.sleep(randint(1, 5))

Categories

Resources