Python Selenium not clicking button - python-3.x

Why would selenium not find the element using xpath?
<input class="btn btn-success" name="submit" id="loginButton" accesskey="l" value="Login" tabindex="6" type="submit">
Using:
driver.find_element_by_xpath("//input[#class='btn btn-success']").click()
I get:
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: //input[#class='btn btn-success']
Tried css selector as well

Try with explicit wait.
WebDriverWait(driver, 10).until(EC.presence_of_element_located(By.CSS_SELECTOR,"input.btn.btn-success")).click()

Try another attribute
driver.find_element_by_xpath("//input[#id='loginButton']").click()

Related

Unable to click expand button using selenium web driver in Python

I'm totally new to programming. Currently trying to automate few daily tasks using selenium web driver on Python. I have a webpage which contains multiple + expand button. Below is the code
Without expansion:
<div class="expansion container">
<div class="expansion_base_parent"></div>
<div class="expansion expansion_parent">
<button type="button" class="compact-visual-toggle"></button>
</div>
</div>
With expansion
<div class="expansion_container">
<div class="expansion_base_parent"></div>
<div class="expansion expansion_parent">
<button type="button" class="compact-visual-toggle active"></button>
</div>
</div>
I'm unable to find this element using any of the find_by method
My colleague said the page contains json that's why unable to locate using find_by
Can somebody please help with the code to locate and click the expand button.
Actual page:
Go-to https://fortigate.fortidemo.com
Username demo
Password demo
Click login read-only
Click later in next window
Now navigate to Network > interfaces There you can see lot of expand button that's what I'm referring I have written code to come till this page, but I want to expand before taking screenshot of the page
It does not have to do anything with JSON.
See, you are saying
<div class="expansion container">
<div class="expansion_base_parent"></div>
<div class="expansion expansion_parent">
<button type="button" class="compact-visual-toggle"></button>
</div>
</div>
that you see this HTML when expand button is present.
You can locate with below css :-
button.compact-visual-toggle
or xpath :
//button[contains(#class, 'compact-visual-toggle')]
and since you have mentioned that they are multiple expension button, you can differentiate like below :
(//button[contains(#class, 'compact-visual-toggle')])[1]
should represent the first, for second you can try :
(//button[contains(#class, 'compact-visual-toggle')])[2]
and so on.. for 3rd, 4th....
in code you can use it like this :
driver.find_element_by_xpath("//button[contains(#class, 'compact-visual-toggle')]").click()
or
driver.find_element_by_xpath("(//button[contains(#class, 'compact-visual-toggle')])[1]").click()
or
driver.find_element_by_xpath("(//button[contains(#class, 'compact-visual-toggle')])[2]").click()
Now, coming to second part :
<div class="expansion_container">
<div class="expansion_base_parent"></div>
<div class="expansion expansion_parent">
<button type="button" class="compact-visual-toggle active"></button>
</div>
</div>
in you you want to un-expand it, you could use the below xpath :
//button[contains(#class, 'compact-visual-toggle active')]
and use it like above.

How can i read html codes with python selenium

I want to get a link in html path, the codes are here:
<div class="btn btn-primary btn-lg">
<a href="https://storage.googleapis.com/audiog-204018.appspot.com/files/hello1574358335.mp3">
Download MP3 </a>
</div>
How can i get this link with selenium
In Python Selenium you can simply use element.get_attribute()
So in your case, it should just be
element.get_attribute("href")

Timeout Waiting for Visible iframe

I'm learning Selenium with Python, and attempting to write happy path flow for a website. Where I get stuck is a part of the flow where an iframe is automatically launched after clicking through a pop-up window. I've tried several different methods, but am unable to locate the frame, or wait for it appear. Either it's not found or times out.
WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.ID, 'continue- reservation'))).click()
# cvv2 form
WebDriverWait(driver, 5).until(EC.frame_to_be_available_and_switch_to_it((By.ID, 'cvv_iframe')))
Error:
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.ID, 'cvv_iframe')))
File "/Users/anutter/venv/lib/python3.7/site-packages/selenium/webdriver/support/wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
HTML is as follows:
<div id="cpstaging" class="emptystage">
<div class="fixed h-v-centered new-preload" style="display: none;"><span class="fa fa-spinner f-50 f-grn fa-pulse"></span></div>
</div>
<div id="cvv-box" class="pad-30-lr">
<div id="enter-cvv-title" class="blk mar-20-b f-18 border-b pad-10-b">Enter CVV Code</div>
<iframe src="https://qa-hotels.ecbsn.com/cvv?oauth_token=u7q99%2Fe8I%2BkUrkLMr4dGR2t4gmcDbVtr&type=visa&src=web-desktop" id="cvv_iframe" name="cvv_iframe" width="430" height="160" frameborder="0" seamless="seamless" scrolling="no" sandbox="allow-same-origin allow-scripts allow-popups allow-forms"></iframe>
</div>
</div>
</div>
</div>
As per the HTML you have shared to access the elements within the <iframe> you have to:
Induce WebDriverWait for the desired frame to be available and switch to it using either of the Locator Strategies:
Using ID:
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"cvv_iframe")))
Using NAME:
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.NAME,"cvv_iframe")))
Using CSS_SELECTOR:
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#cvv_iframe[name='cvv_iframe']")))
Using XPATH:
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#id='cvv_iframe' and #name='cvv_iframe']")))
Note: As per your code trials the ID of the <iframe> isn't cvv-iframe but cvv_iframe
Here you can find a relevant discussion on Ways to deal with #document under iframe

Selenium Webdriver python XPath not working

I am trying to automate a process using Python and selenium webdriver.
I am able to login successfully and navigating to the page where I want to post something but for some reason the xpath is not recognized by the system.
It gives the below error:
NoSuchElementException: Unable to locate element: //*[#id="widecol"]/div/form/p[1]/input
Here is my code:
from selenium import webdriver
mydriver = webdriver.Firefox()
mydriver.get("https://www.phishtank.com/")
mydriver.maximize_window()
mydriver.find_element_by_xpath('//*[#id="username"]').send_keys("myusername")
mydriver.find_element_by_xpath('//*[#id="password"]').send_keys("mypassword")
mydriver.find_element_by_xpath('//*[#id="header"]/div[2]/form/input[3]').click()
mydriver.find_element_by_xpath('//*[#id="nav"]/ul/li[2]/a').click()
mydriver.find_element_by_xpath('//*[#id="widecol"]/div/form/p[1]/input').send_keys("sample text testing to fill form")
This is my HTML code
<div id="widecol">
<div class="padded">
<form method="POST">
<h2>Add A Phish</h2>
<ol>
<li>Visit our <b>What is phishing?</b> page to confirm that the suspected phish meets all of the criteria.</li>
<li>Add a phish using the form below, or even better, submit a phish directly via email.</li>
</ol>
<h3>Phish URL:</h3>
<p>
<input type="text" name="phish_url" style="width:90%;" value="" /><br />
<span class="small">Copy and paste the URL of the phishing website.</span>
</p>
<h3>What is the organization referenced in the email?</h3>
<p class="slim">
<select name="phish_target">
which gives me the following error:
NoSuchElementException: Unable to locate element: //*[#id="widecol"]/div/form/p[1]/input
Here is the HTML code:
<input type="text" name="phish_url" style="width:90%;" value=""> outer HTML code
And this is my XPath:
//*[#id="widecol"]/div/form/p[1]/input - Xpath
Please let me know where to look, thank you.
You need to induce WebDriverWait for the element to be clickable and you can use the following line of code :
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#name='phish_url' and #type='text']"))).send_keys("sample text testing to fill form")
Without the full HTML, we can't tell if the XPath is correct or no.
Can you please try and include more outer Html code?
You can try and wait after clicking on that last URL, maybe the element wasn't loaded yet.
You can also check if the input is in an iframe, that might be causing the problem.
Could you try the following instead?
//*[#id='widecol']//input[#name='phish_url']
Should work for that input. Just for trial/error sake, try a small wait before doing this.
The new Selenium version doesn't allow it.
Try the below code
from selenium.webdriver.common.by import By
And then Change the 'find_element_by_xpath statements' to
mydriver.find_element(By.XPATH,'//[#id="username"]').send_keys("myusername")
You can refer to the link to know more

How do you change the return key text in Mobile Safari keyboard without the form tag?

I would like to change the the "return" button text on the Mobile Safari keyboard when my input element is focused. I know you can do this:
<form action="somewebsite.com">
<input id='SearchTextBox' type="search"/>
<input id='SearchButton' type="button" value="Search" />
</form>
But I don't want the form tag because the search is ajaxian (I don't want to reload the page).
Does anyone know if this is possible?
Heh, should have thought of this (coworker solved). Just cancel the form submission with JavaScript. For example:
<form action="#" onsubmit="return false;">
<input id='SearchTextBox' type="search"/>
<input id='SearchButton' type="button" value="Search" />
</form>
You can now use enterkeyhint
<input enterkeyhint="search" />
Theres more options like "send", "go", etc

Resources