<input class="form__input number-verify" type="tel" id="sms-number" maxlength="20">
tel = browser.find_element(By.ID , "form__input number-verify")
It's not working. I want to click that element and then write (ı know how do it) but ı cant solve my problem.
and that all code.
from selenium import webdriver
from bs4 import BeautifulSoup
from selenium.webdriver.common.by import By
import time
browser = webdriver.Edge()
browser.get("https://globfone.com/send-text/")
soup = BeautifulSoup(browser.page_source, 'html.parser')
giris_yap = browser.find_element(By.NAME,"sender-name")
giris_yap.click()
giris_yap.send_keys("abc")
next_button = browser.find_element(By.CLASS_NAME, "cover__btn")
next_button.click()
time.sleep(5)
tel = browser.find_element(By.ID , "form__input number-verify")
time.sleep(5)
browser.close()
Your main mistake is that form__input number-verify is not ID, these are 2 class values, so you can use CSS Selector or Xpath to use these 2 class names.
Also, you should not use hardcoded pauses. WebDriverWait expected_conditions should be used instead.
The following code works:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument("start-maximized")
webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(service=webdriver_service, options=options)
url = 'https://globfone.com/send-text/'
driver.get(url)
wait = WebDriverWait(driver, 20)
giris_yap = wait.until(EC.element_to_be_clickable((By.NAME, "sender-name")))
giris_yap.click()
giris_yap.send_keys("abc")
wait.until(EC.element_to_be_clickable((By.CLASS_NAME, "cover__btn"))).click()
tel = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".form__input.number-verify")))
tel.send_keys("755")
the result is:
Related
I am trying to have selenium to do the following:
Open a website
Click on the search box
Type "Seattle" in the search box
Select the first result from the suggested results
My code fails at Step 2.
The class id for the search box is "class = input_search ng-pristine ng-valid ng-empty ng-touched"
Here's my code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
url = 'https://wego.here.com/'
driver.get(url)
driver.find_element_by_css_selector('.input_search.ng-pristine.ng-valid.ng-empty.ng-touched').click()
driver.find_element_by_css_selector('.input_search.ng-pristine.ng-valid.ng-empty.ng-touched').send_keys('Seattle')
driver.find_element_by_css_selector('.input_search.ng-pristine.ng-valid.ng-empty.ng-touched').send_keys(Keys.ENTER)
Any suggestions would be greatly appreciated!
ADDITIONAL QUESTION
Thanks to #Prophet I was able to get the first auto-click and auto-fill done, but when I try to do the same task with a different search box, it does not like it. Please refer to the following code that I added to the existing code:
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "button.btn"))).send_keys(Keys.ENTER)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input.itinerary_item_input_0"))).click()
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input.itinerary_item_input_0"))).send_keys('Chicago')
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input.itinerary_item_input_0"))).send_keys(Keys.ENTER)
button.btn did work, but not the input.itinerary_item_input_0. Here is the source screenshot:
You are using a wrong locator.
ng-empty and ng-touched may not be there all the times.
So instead of
driver.find_element_by_css_selector('.input_search.ng-pristine.ng-valid.ng-empty.ng-touched').click()
Try using this:
driver.find_element_by_css_selector('input.input_search').click()
input.input_search is an unique, stable locator for that element.
Also, you have to add a delay, preferably to use the expected conditions, as following:
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
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 20)
url = 'https://wego.here.com/'
driver.get(url)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input.input_search"))).click()
wait=WebDriverWait(driver, 60)
driver.get('https://wego.here.com/')
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"input.input_search"))).send_keys("Seattle")
wait.until(EC.element_to_be_clickable((By.XPATH,"//div[#class='dropdown_list']/div[1]"))).click()
This will select the first option after sending a string to input tag.
Imports:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
I'm using selenium, and in the middle of the automation, this code only works if I manually refresh the browser.
If I do driver.get("URL"), or driver.refresh(), it doesn't help.
from selenium.webdriver.common.action_chains import ActionChains
elems = driver.find_elements_by_tag_name('div')
elem = elems[3]
action = ActionChains(driver)
action.click(elem)
action.send_keys("text")
action.perform()
I already can't use elem.send_keys(), it doesn't work anyway or anyhow. Though elem.click() does, that doesn't help if elem.send_keys() doesn't.
I think you can use explicit wait in this case like this :
from selenium.webdriver.common.action_chains import ActionChains
wait = WebDriverWait(driver, 50)
elems = driver.find_elements_by_tag_name('div')
elem = elems[3]
action = ActionChains(driver).move_to_element(elem).perform()
wait.until(EC.visibility_of((elem))).click()
time.sleep(2)
wait.until(EC.visibility_of((elem))).send_keys("text")
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
I'm trying to extract info from morningstar but driver return back a list empty.
here's the code:
from selenium import webdriver
import time
import datetime
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome(<<...>>)
driver.implicitly_wait(1)
driver.maximize_window()
driver.get('https://www.morningstar.it/it/etf/snapshot/snapshot.aspx?id=0P0001J2VK&tab=3&InvestmentType=FE')
time.sleep(5)
driver.find_element_by_id("individualinvestor").click()
time.sleep(1)
driver.find_element_by_id("_evidon-accept-button").click()
time.sleep(1)
usStock=driver.find_elements_by_xpath('//*[#class="sal-asset-allocation__assetTableRow sal-asset-allocation__assetTableRow--usStock"]/td[2]')
thx
That is because it is in iframe, so you need to switch it first like this :
wait = WebDriverWait(driver, 20)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID, "portfolio")))
usStock = driver.find_elements_by_xpath('//*[#class="sal-asset-allocation__assetTableRow sal-asset-allocation__assetTableRow--usStock"]/td[2]')
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
also, I would scrape it like this (since time.sleep() is not recommended at all) so you can use Explicit wait like this :
driver = webdriver.Chrome(driver_path)
driver.maximize_window()
driver.implicitly_wait(30)
driver.get("https://www.morningstar.it/it/etf/snapshot/snapshot.aspx?id=0P0001J2VK&tab=3&InvestmentType=FE")
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.ID, "individualinvestor"))).click()
wait.until(EC.element_to_be_clickable((By.ID, "_evidon-accept-button"))).click()
wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID, "portfolio")))
usStock = driver.find_elements_by_css_selector("tr[class*='al-asset-allocation'] td")
print(len(usStock))
for stock in usStock:
print(stock.text)
I am creating a .py script to automate queries to the World Bank data repository using Selenium.
The script will check boxes from the "Countries", "Series", and "Time" menus. I don't have problems selecting arrays of countries.
country_codes = ['AUT', 'BRA', etc.]
for country in country_codes:
country_xpath = '//*[#id="chk[Stat_Ctry_Ext].[List].&[' + str(country) + ']"]'
driver.find_element_by_xpath(country_xpath).click()
However, selecting series raises a NoSuchElementException error.
driver.find_element_by_xpath('//*[#id="chk[STATS_Series_Ext].[Topic].&[UIS.NERA.2.M]"]')
Image: Web Inspector
Please try the below code and let me know if you face any issue -
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
import time
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 20)
action = ActionChains(driver)
driver.get('https://databank.worldbank.org/source/education-statistics')
time.sleep(5)
country_codes = ['AFG', 'AUT', 'BRA']
for country in country_codes:
driver.find_element_by_xpath('//*[#id="chk[Stat_Ctry_Ext].[List].&[' + country + ']"]').click()
# Click on the series tab
driver.find_element_by_xpath('(//a[contains(#title,"Series")])[1]').click()
Series_UIS_NERA_2_M = wait.until(
EC.visibility_of_element_located((By.XPATH, '//*[#id="chk[STATS_Series_Ext].[Topic].&[UIS.NERA.2.M]"]')))
driver.find_element_by_xpath('//*[#id="chk[STATS_Series_Ext].[Topic].&[UIS.NERA.2.M]"]').click()
I have been trying to have my selenium code click on the search in the sidebar menu and open the search page. But I have had no luck getting it right. All I get is selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"div[class*=sidebar-wrapper]"}
Here is how the HTML looks like:
Here my code:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.select import Select
from selenium.webdriver import ActionChains
driver = webdriver.Chrome()
driver.get("https://xxxx.com/Login")
username = driver.find_element_by_id("username")
username.clear()
username.send_keys("xxxxx")
password = driver.find_element_by_name("password")
password.clear()
password.send_keys("yyyyyy")
driver.find_element_by_name("login").click()
import time
time.sleep( 5 )
new=driver.find_element_by_xpath("/html/body/div/div[1]/div/ul/li[9]/a")
newpage=new.click()
time.sleep( 10 )
**I tried many different combinations such as:**
#driver.find_element_by_css_selector("div[class*=sidebar-wrapper]").get(3).click()
#driver.find_element_by_xpath("/html/body/div[1]/div[1]/div/div[1]/div/div/div[1]/div/div[2]/a").click()
#driver.find_element_by_class_name("#sidebar-wrapper > div > div > div.top > div > div:nth-child(2) > a > span.page-name").click()
I am not able to figure out where my mistake is when selecting the element. any help is appreciated.
Try below code -
wait = WebDriverWait(driver, 20)
MyLink = wait.until(EC.visibility_of_element_located((By.XPATH, '//div[#class="top"]/div[#class="pages"]/div[2]/a')))
MyLink.click()
list of imports -
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
You can also try the below code -
WebElement elem = driver.findElement(By.xpath("element_xpath"));
((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView();", elem);
One More method you can try is -
action = ActionChains(driver)
action.move_to_element(yourElement).click().perform()
Let me know the outcome.