How do I use the Selenium Click function - python

I am trying to click a login button after filling out all the information for the website. So far I have been able to fill in spaces using driver.find_element("id","username").send_keys("username") when I use driver.find_element_id("username").send_keys("username") it wont work.
The login button elements are class=radius and type=sumbit also i class="fa fa-2x fa-sign-in"
(I would just link the site but I dont know if that would be against TOS)
So far I have used the following commands
element = driver.find_element_by_link_text("Login")
element.click()
driver.find_element_by_xpath(//*[#id="login"]/button.click() but xpath gives an "unresolved reference"
I am using IDE PyCharm Build #PC-222.4167.33 on Windows

Try this code:
driver.find_element(By.ID, "username").send_keys("<enter the username>")
driver.find_element(By.ID, "password").send_keys("<enter the password>")
time.sleep(1)
driver.find_element(By.CSS_SELECTOR, ".radius").click()
time.sleep(2)
driver.find_element(By.CSS_SELECTOR, ".button.secondary.radius").click()
time.sleep(2)
I have added 'time.sleep()' just to show you it is working correctly, avoid using it in your real-time projects.

Looks like the same problem I solve here Selenium, selecting from dropdown
You should localize the element with XPath or CSS selector, i recommend you try with CSS selector, if don't work try with xpath .

Related

How to click buttons on amazon with selenium?

I'm trying to click buttons on amazon with selenium (python) but it won't work. It says the elements don't exist. I tried to google it but only found outdated solutions. I've tried Xpaths,ID,full XPATH on pretty much all input fields with no success. I've used selenium before and it works flawlessly but on amazon in particular it can't find the elements. For example the place order button (Je bestelling plaatsen in my language) can't be found nor clicked.
driver.find_element(By.XPATH, '//*[#id="turbo-checkout-pyo-button"]/span/input')
driver.find_element(By.ID, 'turbo-checkout-pyo-button')
driver.find_element(By.XPATH, '/html/body/div[4]/div[1]/div/div/div/div[2]/div/form/div/span/span/span/input')
How do I know which elements are clickable and direct to the intended page? If there is a way to tell please let me know. Thanks in advance
I'm not a selenium master but you can use the find_element_by_xpath function that returns an element object that can be clicked so:
element = driver.find_element_by_xpath('/html/body/div[4]/div[1]/div/div/div/div[2]/div/form/div/span/span/span/input')
element.click()
The problem was that the button was located in an iframe. Switching to the iframe and then pressing the button does the job. Found out through this question
driver.switch_to.frame("turbo-checkout-iframe")
directButton = driver.find_element(By.CSS_SELECTOR, '#turbo-checkout-pyo-button')
directButton.click()

Why won't Selenium in Python click the pop-up privacy button?

I am having some issues with Selenium not clicking the pop-up privacy button on https://www.transfermarkt.com/
Here is my code so far:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://www.transfermarkt.com/')
accept_button = driver.find_element_by_xpath('/html/body/div/div[2]/div[3]/div[2]/button')
accept_button.click()
It comes up saying:
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div/div[2]/div[3]/div[2]/button"}
Anyone have any thoughts?
I believe the issue is that the element you are trying to click is inside an iframe. In order to click that element you'll have to first switch to that frame. I noticed the iframe has title="SP Consent Message" so I'll use a CSS Selector to identify it based on that. Your code with the added line:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://www.transfermarkt.com/')
driver.switch_to.frame(driver.find_element_by_css_selector('iframe[title="SP Consent Message"]'))
accept_button = driver.find_element_by_xpath('/html/body/div/div[2]/div[3]/div[2]/button')
accept_button.click()
Note that you may have to switch back to the default frame to continue your test, I'm not 100% sure as that iframe has gone away.
Also it seems like some folks don't get that popup when the hit the website, not sure why, may be something you want to look in to to determine how you want to test this.
Like #Prophet says you should improve the xpath for the button (again it seems to have a unique title so I would use CSS Selector 'button[title="ACCEPT ALL"]'), but this works for me to click it.
There are 2 issues here:
You need to add wait / delay before accept_button = driver.find_element_by_xpath('/html/body/div/div[2]/div[3]/div[2]/button') to let the page load
It's very, very bad practice to use absolute XPaths. You have to define an unique, short and clear related XPath expression.
Additionally, I see no pop-up privacy button when I open that page. So, maybe that element is indeed not there.

How to click a button in selenium python

I am using Python script to open a web page, then a tab and then i want to click on a button. I am stuck on the last part. I am unable to click the find button. Here is the HTML code when i am using inspect in chrome.
input value="Find" class="cuesButton" name="findButton"
onclick="javascript:onFindSubmit()" type="button"
Here is the button i am trying to click:
I tried driver.find_element_by_name, element_by_id. It says method css selector doesnt have this element by name but it still fails.
2 things can be happening, 1 the page isn’t finished loading, to wait use WebDriverWait like so:
wait = WebDriverWait(driver, 10)
Or 2, the element is in an iframe , in this case you would need to switch to it like so:
driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))
You can switch using xpath, name, cssselector, and tag name

How to locate the element as per the html provided through Selenium, Python and Xpath

I'm using selenium to fill a webform, but this particular textbox for some reason is giving me trouble. The html for it is:
<input type="text" class="black_ar" value="" size="25" id="type" name="type"
onmouseover="showTip(this.id)" onmouseout="hideTip(this.id)"
onblur="trimByAutoTag(this);onSubTypeChangeUnit('className',this,'unitSpan')"
autocomplete="off" title="">
I have used:
driver.find_element_by_name('type').click()
and
driver.find_element_by_xpath('xpath').click()
But they both say that the element cannot be found. I am not sure if it is in an iframe or not (if anyone knows a way to check) and am using chropath which is a chrome extension to find the xpath of the element.
Edit: chropath says under the relative xpath "It might be child of svg/pseudo/comment/iframe. XPath doesn't support for them." if that helps
As per the HTML you have shared to click within the textbox you can use either of the following solutions:
xpath:
driver.find_element_by_xpath("//input[#class='black_ar' and #id='type']").click()
css_selector:
driver.find_element_by_css_selector("input.black_ar#type").click()
Now ChroPath supports iframe. So if you inspect any element and that element is inside iframe, ChroPath will tell that element is inside iframe as well as it will generate the relative xpath.
Open your page in browser let say chrome. Press right click on textbox and select inspect menu. In oped dev tools look if your textbox has parent iframe element. One way to do it:
Click inside chrome dev tools and press ctrl+f
In opened search field copy iframe input.black_ar
If does you have first switch to this frame function for switching frames in python, selenium
To locate your input use driver.find_element_by_css_selector(‘input.black_ar’)
Have you tried, driver.find_element_by_xpath("//input[#id='type']").click(); ?
You could all check by press F12 while in your browser, or going to developer tool; press Ctrl+f afterwards and type in //iframe and try to see if it exists in the page.

Python Selenium button click

I am trying to click a button to show more comments on a discusson thread on http://disqus.com/
The HTML looks like following:
<div class="load-more" data-role="more" style="">
Show more
</div>
I have tried using the xPath from the button like following:
driver = webdriver.Chrome("/usr/lib/chromium-browser/chromedriver")
driver.get(url)
driver.find_element_by_xpath('''//*[#id="posts"]/div[3]/a''').click()
But I get the NoSuchElement exception.
What is the proper way of clicking a button with that type of HTML?
UPDATE: Worked when I switched to a specific iFrame:
driver.switch_to.frame('myID')
Then loaded by class name:
element = driver.find_element_by_class_name('load-more')
element.click()
NOTE: That the click() did not work when it was performed on the same line like driver.find_element_by_class_name('load-more').click()
Xpath should be a last resort for finding elements. For your case, I would try
driver.find_element_by_class_name("load-more__button"));
Using btn load-more__button won't work as compound classNames aren't valid.
If that still doesn't work, Id recommend using css selectors before Xpath.
However, if none of these options work, and your Xpath is still not working, you should use the firebug add on for Firefox to ensure the Xpath you are using is correct.
Here's some of my other answers that may help you get started with Firebug:
https://stackoverflow.com/a/38980488/3537915
https://stackoverflow.com/a/38723782/3537915
https://stackoverflow.com/a/38744577/3537915

Categories

Resources