why 2nd click action is not run when browser is Google Chrome - python

I don't konw why 2nd click action is not be run when browser is Google Chrome.
The 1st input action: input text has been done, then 2nd click action failed to be run
And there's nothing happened on the UI.
Could anybody can tell me the reason?
test link:https://www-01.ibm.com/products/hardware/configurator/americas/bhui/launchNI.wss
from selenium import webdriver driver=webdriver.Chrome()
driver.get('https://www-01.ibm.com/products/hardware/configurator/americas/bhui/launchNI.wss') driver.find_element_by_id("modelnumber").send_keys("7383AC1")
driver.find_element_by_name("submit").click()

That page has 3 elements with the name "submit", so ChromeDriver is probably trying to click one that you're not expecting.
Try finding by xpath or css. I'm more familiar with XPath:
driver.find_element_by_xpath("//input[#id='modelnumber']/../following-sibling::td/input[#name='submit']").click()

Related

I am having trouble clicking the signup button on a reddit sign up page with selenium using python

I am trying to submit a form to create a new reddit account using selenium at https://www.reddit.com/reddits/login/ . I am new to using selenium and web scraping in general and need some help selecting and ultimately clicking the sign up button on this webpage after all the details have been entered in. I have tried to select it using
submit = browser.find_element(By.CSS_SELECTOR("//*[#id="register form"]/div[7]/button")).text
along with trying to grab it through its xpath which is
//*[#id="register-form"]/div[7]/button
but am having no luck. The farthest I am able to get is having it say a 'str' object is not callable using Selenium through Python as an error message or it will say element not found. Any help would or guidance in the right direction would be greatly appreciated.
I am able to enter in all fields of data and beat the re-captcha that pops up also and be entirely good to go except for this sign up button. I am not familiar with this much so I am not sure why any of the responses answers worked when I tried them. I am not sure if it is something about the button or if its just how the page is setup or if its needing to run some javascript with the button click. I am not sure why this button cannot be clicked.
To solve the re-captcha I load another webpage and delete it to get back to this one not sure if this is relevant or not.
Picture of the Button I am trying to click on with selenium
Sign Up Button Picture
SOLVED: All of these solution below work for accessing the button. My mistake was switching to a different tab and not properly switching back. So I was trying to click an element on an old tab not the main sign in page. Once the focus was shifted back to that page the element was found and clicked!
The CSS Selector for that submit button would be:
"#register-form button"
You can verify from the console of a web browser by calling:
document.querySelector("#register-form button")
XPATH worked for me:
url = driver.find_element(By.XPATH , '//*[#id="register-form"]/div[7]/button')
url.click()
wait = WebDriverWait(driver, 30)
driver.get("https://www.reddit.com/reddits/login/")
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,'#user_reg'))).send_keys('user')
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,'#passwd_reg'))).send_keys('pw')
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,'#passwd2_reg'))).send_keys('pw')
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,'#email_reg'))).send_keys('user#email.com')
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,'#register-form > div.c-clearfix.c-submit-group > button'))).click()
The full signup for that would be like so you would want to click the element of button. Replace keys with values you need.
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 not recognizing the xpath

On Python 3.9 and Selenium 4.00
Hi there, I'm currently trying to automate downloading a few things on Chrome. I got the login part and navigating to the page down and it works properly. I'm having issues with the next part which is clicking "export" then "export as csv". I hover over the HTML source code and it highlights the buttons I need to press so I hit "copy XPath" but selenium won't press it and I get this error.
Edit: I cannot share the site as it is locked behind a login and it is not my login to give out; end of edit.
Message: invalid selector: Unable to locate an element with the xpath expression //*[#id="report_nav_menu"]/ul/li[2]/a"
Here's my code
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get('website')
driver.find_element(By.XPATH, '//*[#id="report_nav_menu"]/ul/li[2]/a').click()
time.sleep(1) # makes sure the page loads
driver.find_element(By.XPATH, '//*[#id="report_nav_menu"]/ul/li[2]/ul/li[6]/a').click()
time.sleep(1000) # to keep the browser open
This the is HTML source code:
Source code
The first highlight in the pic is for the Export button.
Need to click this first
The second highlight shows that it's for the CSV button.
Need to click this second
//class[#elname="zc-navmenuEl/button[2] seems to be an invalid XPath expression.
I can't see this locator used in the code you presented in the question.
Also you didn't share an URL of the page you are working with so I can't determine the correct element locator.

How to close iframe in selenium

So, I'm working on a Selenium webdriver Project, and, in one of the process, I needed to enter in na iframe, and then, send Keys to the input in there, but, now I want to close or exit that iframe to can click in a button outside that frame, check out me code piece:
WebDriverWait(bot,20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe.top[title='Registration form']")))
WebDriverWait(bot, 20).until(EC.element_to_be_clickable((By.XPATH,'//*[#id="username"]'))).send_keys(self.usernameChar)
WebDriverWait(bot, 20).until(EC.element_to_be_clickable((By.XPATH,'//*[#id="username"]'))).send_keys(Keys.RETURN)
Can you guys help me?
Note: My google version is the 85
To exit from any <iframe> and set Selenium's focus on the Top Browsing Context you can use the following line of code:
driver.switch_to.default_content()
Reference
You can find a couple of relevant discussions in:
How to send text to the Password field within https://mail.protonmail.com registration page?

Selenium will open chrome tab but not find URL

Selenium starts a blank chrome tab but does not go to the specified link. I would Imagine its a fairly simple fix. My selenium is up to date.
My Code
Error Output
try the below command
browser.get('https://www.google.com')

python selenium youtube like button

So I am trying for over an hour now to make selenium click the youtube like button...
Nothing works on google and I have no idea what to do anymore.
If someone can help me that would be amazing
(Im kinda new to python)
Thanks!
So following on my comment, you need to inspect the xpath of the youtube like button.
Right click on the like button, press inspect element. It should show a console and in the console the like button path should be highlight. Click on it then click copy xpath.
After that do what you need to do, e.g like_button_click = driver.find_element_by_xpath('xpath of the like button').click()
Selenium docs: https://www.seleniumhq.org/docs/
The below is tested and working - remember that to like videos, you always need to be logged into your Youtube account.
1) Identify the buttons xPath:
To extract the specific Like button xPath, you can inspect the button through any developer browser tool, right click on it and select Copy xPath:
Then just copy the xPath into here:
button = driver.find_element_by_xpath("here")
2) Log into Youtube and click on the Like button
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from time import sleep
driver = webdriver.Chrome()
youtube = driver.get("https://www.youtube.com/watch?v=9fHt-VVG_hg")
sleep(5)
button = driver.find_element_by_xpath("//*[#id='top-level-buttons']/ytd-toggle-button-renderer[1]/a")
ActionChains(driver).move_to_element(button).click(button).perform()
Try this one
//yt-animated-icon[contains(#animated-icon-type,"LIKE")]

Categories