I am a learning python right now and try to finalize my second script.
I know how to click on an element by Id, text and ngclick from selenium - but none of these worked for my current one so far :/
This element is completely new to me - I am trying to click on this button:
<button type="submit" class="btn btn-primary">Submit</button>
Found something in the previous questions which is
driver.find_element_by_css_selector("btn").click()
but have no idea what driver is referring to :-)
Can somebody help a drowning beginner?^^
Best
Vanassins
Steps:
Create a WebDriver instance.
Navigate to a Web page.
Locate an HTML element from UI.
Perform an action on an HTML element.
Run tests and record test results using a test framework.
Sample Example:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Firefox()
driver.get("http://www.google.com")
input_element = driver.find_element_by_name("q")
input_element.send_keys("Test Selenium")
input_element.submit()
Related
I am new to selenium and I try to navigate on a page and to click on a button to get to the next page with selenium web driver.
This is my python code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.XPATH, '//*[#id="page"]/div/div/div[5]/div/div/div/div[3]/div/span[2]')))
driver.find_element(By.XPATH, '//*[#id="page"]/div/div/div[5]/div/div/div/div[3]/div/span[2]').send_keys(Keys.RETURN)
print('Element found')
In addition to send_keys(Keys.RETURN) I also tried send_keys(Keys.ENTER) and click(). In all cases the statement "Element found" is printed to the console, but nothing happens on the webpage.
Here is the HTML bit of the button:
<span class="ACME-src-ACME-ui-Pagination--arrowRight">
<i class="flaticon-right-arrow"></I>
</span>
Every help is highly appreciated.
Wait!! Looks like The Xpath you are using is incorrect. Spans are not clickable objects. Or like they are clickable but do not do anything, which is exactly happening here. May be the "i" tag is something which should take click(However I cannot see any object in Dom taking click and performing a job)
First provide me with the proper DOM.
You can further try two more ways of doing it:-
button = find_element(By.XPATH, '//*[#id="page"]/div/div/div[5]/div/div/div/div[3]/div/span[2]/i')
driver.execute_script("arguments[0].click();", button)
OR
button = find_element(By.XPATH, '//*[#id="page"]/div/div/div[5]/div/div/div/div[3]/div/span[2]/i')
ActionChains(driver).move_to_element(button).click().perform()
I am trying to automate the location selection process, however I am struggle with it.
So for, I can only open the menu and select the first item.
And my code is:
import bs4
from bs4 import BeautifulSoup
import selenium
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome(ChromeDriverManager().install())
url = 'https://www.ebay.com/b/Food-Beverages/14308/bn_1642947?listingOnly=1&rt=nc'
driver.get(url)
soup = BeautifulSoup(driver.page_source, 'html.parser')
button = driver.find_element_by_id('gh-shipto-click') //find the location button
button.click()
button2 = driver.find_element_by_id('gh-shipto-click-body-cnt') //open the menu
button2.click()
driver.find_element(By.XPATH,"//div[#role='menuitemradio']").click() //choose the first location
I believe the attribute "data-makeup-index" (show in the pic) would help, but I don't know how to use it.
Sine some of you may not able to find the "ship to" button. Here is the html code I copied from the web.
<li id="gh-shipto-click" class="gh-eb-li">
<div class="gh-menu">
<button _sp="m570.l46241" title="Ship to" class="gh-eb-li-a gh-icon" aria-expanded="false" aria-controls="gh-shipto-click-o" aria-label="Ship to Afghanistan"><i class="flgspr gh-flag-bg flaaf"></i><span>Ship to</span></button>
<i class="flgspr gh-flag-bg flaaf"></i>
I have found my answer as below:
driver.find_element(By.XPATH,"//div[#role='menuitemradio'][{an integer}]").click()
Although the Ship to was not visible to my location, I've inspected it from different Geo location and guess what I was able to find this elementstyle="display: none;
I've removed the none temporarily, and it got displayed.
<li id="gh-shipto-click" class="gh-eb-li gh-modal-active" style="display:none;" aria-hidden="false">
You could find the element by these XPath and handle the dropdown
#This XPath is taking you to the specific div where you need to provide the element index [1][2][3] the ID of this XPath is dynamic
driver.find_element_by_xpath("((((//*[contains(#id,'nid-')])[2])//child::div/div")
driver.find_element_by_xpath("//*[#class='cn']")
I know you got your solution, but this what I've tried It might help
I am trying to do a tutorial and learn Selenium in python however i cant seem to get Selenium to click the "Checkout" button using "element_to_be_clickable((By.XPATH".
I am using:
Python v3.9
Chrome v87
This is the URL i am practicing on:
https://www.aria.co.uk/myAria/ShoppingBasket
And this is my current code for the clicking:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
import time
# Open Chromedriver
driver = webdriver.Chrome(r"C:\Users\Ste1337\Desktop\chromedriver\chromedriver.exe")
# Open webpage
driver.get("https://www.aria.co.uk/SuperSpecials/Other+products/ASUS+ROG+Pugio+2+Wireless+Optical+RGB+Gaming+Mouse?productId=72427")
#https://www.aria.co.uk/Products/Components/Graphics+Cards/NVIDIA+GeForce/GeForce+RTX+3060+Ti/Palit+GeForce+RTX+3060+Ti+Dual+8GB+GPU?productId=73054
# Click "Add to Basket" or refresh page if out of stock
try:
element = WebDriverWait(driver, 1).until(EC.presence_of_element_located((By.XPATH, "Out of Stock!")))
time.sleep(5)
browser.refresh()
except:
button = driver.find_element_by_id("addQuantityButton")
button.click()
basket = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.ID, "basketContent")))
basket.click()
checkout = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH("//img[contains(#src,'/static/images/checkoutv2.png.png')]"))).click()
I can see your xpath is not correct.
Your Xpath should be.
//img[contains(#src,'/static/images/checkoutv2.png')]
Your code should be.
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//img[contains(#src,'/static/images/checkoutv2.png')]"))).click()
The link you provided contains hCaptcha, which is actually responsible to check whether you are a human being or a bot. I guess that it's also the reason, why you can't click any of the items on the page, because Selenium actually is nothing less than a bot.
You first have to pass the test by clicking on the images, which are asked for.
I am attempting to make a web scraper for the Cisco Webex control hub. Since the login page and subsequent pages use Javascript I am using the Selenium Python library to accomplish this. Unfortunately, I am unable to find the text box and sign in button elements with Selenium.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
# ブラウザを開く。
driver = webdriver.Chrome()
driver.get('https://admin.webex.com')
driver.find_element_by_class_name('md-input ng-pristine ng-invalid ng-touched').send_keys('email#address.com')
driver.find_element_by_class_name('md-panel__cta').click()
I don't have much code yet, but I have tried every way to find the elements, but nothing I tried has worked. Is there any reason why I can't find the elements?
I have moved on ahead and finished the code for the password entry page. However, how Cisco has made each page is different so I was unable to reuse the code from the password entry section on the username entry code.
Your issue was page loading. Induce some waits or you'll miss the element.
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "input.md-input"))).send_keys("email#address.com")
Import
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
when an element has more than one class, it will not be a good idea to locate element with class like this find_element_by_class_name('md-input ng-pristine ng-invalid ng-touched').
In this case, xpath or cssSelector is a good choice:
driver.find_element_by_xpath("//input[#name='email']")
I am working on web scraping task using selenium and stuck at click function.
Steps in Website:
1. Open Website
2. Enter Key value in the search text box
3. Click on Search to start the search process
After step 3 it is supposed to load the progress bar and start searching for results.
However, after clicking on search, Progress Bar appears for a second and goes away.
My code:
browser = webdriver.Chrome(executable_path='C:/Chrome/chromedriver.exe')
browser.set_page_load_timeout(30000)
browser.get("labs.nccgroup.trust/typofinder/")
browser.find_element_by_id('host').send_keys("example.com")
elem=browser.find_element_by_xpath("//*[#id='typogulator']/input[2]")
elem.click()
Try Use WebDriverWait after inserting value in search field.
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
browser = webdriver.Chrome()
browser.get('https://labs.nccgroup.trust/typofinder')
browser.find_element_by_id('host').send_keys("example.com")
ele=WebDriverWait(browser,20).until(EC.element_to_be_clickable((By.XPATH,"//input[#type='submit'][#value='Search']")))
ele.click()