how to press enter in selenium python with out any error? - python-3.x

I want to search a special keyword in Instagram. For example, I want to search this word:"internet". I can send this key in search box. But, when I use submit method in Selenium by Python3, it doesn't work and give me error. This is my code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.alert import Alert
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC
import time
driver = webdriver.Firefox()
url="https://www.instagram.com/p/pTPI-kyX7g/?tagged=resurant"
driver.get(url)
#login_button=driver.find_element_by_xpath("""/html/body/span/section/main/article/div[2]/div[2]/p/a""")
#login_button.click()
import time
driver.implicitly_wait(5)
search_button=driver.find_element_by_xpath("""/html/body/span/section/nav/div[2]/div/div/div[2]/input""")
search_button.send_keys("internet",Keys.RETURN)
This is no anu error, but it dosen't work.

By this code enter will be pressed, but, after pressing and loading page an error will be occurred:
search_button.send_keys("internet")
while True:
search_button.send_keys(u'\ue007')

Have you tried
searchbox driver.find_element_by_xpath( Xpath/locator for search box).sendKey("internet")
search button driver.find_element_by_xpath(Xpath/locator for the search key button).click
which is you send the "internet" to that edit Box first then looking for that search button and perform .click
not input the search text and send the key at sametime

Use this solution please:
time.sleep(5)
search_button.send_keys(u'\ue007')
search_button.send_keys(u'\ue007')

Related

How to mouse over with Python Selenium 4.3.0?

ENV
Python 3.10
Selenium 4
Hi,
I use :
def findAllByXPath(p_driver, xpath, time=5):
try:
#return WebDriverWait(p_driver, time).until(EC.presence_of_all_elements_located((By.XPATH, (xpath))))
return WebDriverWait(p_driver, time).until(EC.presence_of_all_elements_located((By.XPATH, xpath)))
except Exception as ex:
print(f"ERROR findAllByXPath : {ex}")
posts = findAllByXPath(p_driver,"//article//a",2)
for index in range(0,len(posts)):
p_driver.execute_script("arguments[0].scrollIntoView()", posts[index])
time.sleep(random.uniform(0.5, 0.8))
action.move_to_element(posts[index]).perform()
To make a mouse over and try to get elements.
I am trying to get the number of comments on an Instagram post from a page profile.
If you pass manually the mouse over a post, the number of comments or likes is displayed.
But when I try to do it in python, it doesn't find the element.
I tried the XPath from console
$x(//article//li//span[string-length(text())>0]")
It gives results when I freeze the browser with F8.
action.move_to_element(post).perform() doesn't work? I suspect this version 4.3.0 removed many functions like ActionChains from the Selenium version 3, didn't it?
How can I extract this element?
Or Am I doing something wrong?
ActionChains works nicely in Selenium v4!
I don't have an Instagram account so I cannot try there but you can see by running this code that move_to_element() works and how it works.
The About button (top left corner of stackoverflow homepage) get highlighted.
Remember to don't move your mouse while running of course!
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver = webdriver.Firefox()
actions = ActionChains(driver)
driver.get("https://stackoverflow.com/")
about_button = WebDriverWait(driver, 30).until(EC.visibility_of_element_located((By.XPATH, "//a[#href='https://stackoverflow.co/']")))
actions.move_to_element(about_button)
actions.perform()
EDIT: I am using Selenium 4.4.3 and Python 3.10 but it should work with Selenium 4.3.0 as well.

How find an specific element of page in Selenium python, because I can't find them

I have tried all ways that I knew, but can't found elements "login" and "password". Could someone help me, please? I am tired...
link: https://trading.finam.ru/
from selenium import webdriver
import traceback
from time import sleep
url = 'https://trading.finam.ru/'
driver = webdriver.Chrome(executable_path=r"C:\Users\Idensas\PycharmProjects\pythonProject\selenium\chromedriver.exe")
driver.get(url)
sleep(2)
try:
x = driver.find_element_by_xpath('//label[contains(text(),"Логин")]')
x.send_keys('123')
except Exception:
traceback.print_exc()
finally:
driver.close()
driver.quit()
The picture of that site
The following xpaths will work:
//input[#name='login']
//input[#name='password']
I see the site is loaded slowly so you must put some delay, preferably explicit wait there like:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait.until(EC.visibility_of_element_located((By.XPATH, "//input[#name='login']")))
before trying to insert a texts there
Any website takes sometime to completely load and all the elements are visible to user.
To check the time take taken by your website in different regions you run a check on https://gtmetrix.com/
You can use the simple sleep for few secs until the page is loaded
from selenium import webdriver
import time
driver=webdriver.Chrome(//chromedriver_path_in_your_local//)
driver.get("https://trading.finam.ru/")
time.sleep(5)
login=driver.find_element_by_xpath("//input[#name='login']")
password=driver.find_element_by_xpath("//input[#name='password']")

Python Selenium - Select list item from unordered list

Website: https://vahan.parivahan.gov.in/vahan4dashboard/vahan/view/reportview.xhtml'
I'm trying to use selenium to download data from this website but it's setup in a confusing way. I need to figure out how to use the dropdown in the list called 'Y-Axis' and select 'Maker' from that list. Then I need to hit the 'refresh' button and the 'download excel' button. This is the html of the dropdown menu:
<select id="yaxisVar_input" name="yaxisVar_input" tabindex="-1" aria-hidden="true" onchange="PrimeFaces.ab({s:"yaxisVar",e:"change",f:"masterLayout_formlogin",p:"yaxisVar",u:"xaxisVar"});"><option value="Vehicle Category" data-escape="true">Vehicle Category</option><option value="Vehicle Class" selected="selected" data-escape="true">Vehicle Class</option><option value="Norms" data-escape="true">Norms</option><option value="Fuel" data-escape="true">Fuel</option><option value="Maker" data-escape="true">Maker</option></select>
This is the code I'm playing around with:
from selenium import webdriver
from selenium.webdriver.support.ui import Select
driver = webdriver.Chrome('C:/Users/abhay.singh/chromedriver')
driver.get('https://vahan.parivahan.gov.in/vahan4dashboard/vahan/view/reportview.xhtml')
# Get the y-axis selector
# select = Select(driver.find_element_by_id('yaxisVar_input'))
# select.select_by_visible_text('Maker').click()
# print(select.options)
# print([o.text for o in select.options])
driver.find_element_by_xpath("//select[#name='yaxisVar_input']/option[text()='Maker']").click()
I'd appreciate your help in figuring this out!
This is the best I could do. One thing to note is that selenium is not really meant for downloading, so I added a sleep at the end to ensure the download completes. It can also be done with scripts to monitor the download status, but I don't really know how to do it. I also had to add a sleep in the middle to make sure the "Maker" click was being captured correctly. I'm sure there is a better way to do that as well.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
with webdriver.Chrome() as driver:
driver.get("https://vahan.parivahan.gov.in/vahan4dashboard/vahan/view/reportview.xhtml")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "label[id='yaxisVar_label']"))).click()
time.sleep(2)
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "li[data-label='Maker']"))).click()
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "j_idt61"))).click()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "vchgroupTable:xls"))).click()
time.sleep(10)
You might be able to do this with a requests.post(). I didn't look into the headers or form data, but it is there.

how to press enter in selenium python?

I want to search a special keyword in Instagram. For example, I want to search this word:"fast food". I can send this key in search box. But, when I use submit method in Selenium by Python3, it doesn't work and give me error. This is my code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.alert import Alert
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC
import time
driver = webdriver.Firefox()
url="https://www.instagram.com/p/pTPI-kyX7g/?tagged=resurant"
driver.get(url)
#login_button=driver.find_element_by_xpath("""/html/body/span/section/main/article/div[2]/div[2]/p/a""")
#login_button.click()
import time
driver.implicitly_wait(5)
search_button=driver.find_element_by_xpath("""/html/body/span/section/nav/div[2]/div/div/div[2]/input""")
search_button.send_keys("fast food")
search_button.submit()
This is gave error:
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: ./ancestor-or-self::form
Could you help me?
Instead of. submit() try '.send_keys(u'\ue007')'
See: http://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.common.keys
It need more clicks:
search_button=driver.find_element_by_xpath("""/html/body/span/section/nav/div[2]/div/div/div[2]/input""")
search_button.send_keys("fast")
while True:
search_button.send_keys(u'\ue007')
That is very interesting. Another solution is better. It is very important to know that, you must press 2 Enter press on Instagram search box. So, for eliminating while loop, you can use this code:
search_button.send_keys("#fast")
time.sleep(5)
search_button.send_keys(u'\ue007')
search_button.send_keys(u'\ue007')

ElementNotVisibleException Python/Selenium error for next page button click

I am trying to figure out how to use selenium to perform a next page click on a news release page. Here is my code that will go to the correct website and perform a search to obtain the correct news release articles topic page. This site is configured so that to see every news release on page 1 after the search has executed you must also select the More News Results button at the bottom of the page. I am able to get to the full page 1 news section without problem. Here is my code that performs the search and page clicks.
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('http://www.businesswire.com/portal/site/home/')
search_box_element = browser.find_element_by_id('bw-search-input')
search_box_element.clear()
search_box_element.send_keys('biotechnology')
search_box_element.send_keys(Keys.ENTER)
search_box_element_two = browser.find_element_by_id('more-news-results')
search_box_element_two.click()
That part of the code works fine but I want to be able to click on the next button to move to page 2 and then page 3 and so on. Here is the code that I thought would work but it doesn't:
next_page_click_element = browser.find_element_by_class_name("bw-paging-next")
next_page_click_element.click()
This portion of the code throws an error:
selenium.common.exceptions.ElementNotVisibleException: Message:
element not visible
I have also tried using
next_page_click_element = browser.find_element_by_xpath('//*[#id="more-news-pagination"]/div/div[1]/div/a')
but got the same error message. I have also tried using a wait by adding these lines of codes just before the next_page_click_element section.
element_present = EC.presence_of_element_located((By.ID, "bw-paging-next"))
WebDriverWait(browser, 10).until(element_present)
While this does cause the program to wait it returns this error message:
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
Any suggestions on how to resolve this issue would be greatly appreciated.
Here is the Answer to your Question:
A few words about the solution-
As the Page 2,Page 3,Next etc elements are at the bottom of the page, you have to scroll down to bring those elements within the Viewport.
Once you scroll down you may consider to induce some ExplicitWait for the elements to be located properly on the HTML DOM.
Here is your own code with some simple tweaks which will finally scroll to the bottom of the page and click on Next link:
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.common.by import By
browser=webdriver.Chrome("C:\\Utility\\BrowserDrivers\\chromedriver.exe")
browser.get('http://www.businesswire.com/portal/site/home/')
search_box_element = browser.find_element_by_id('bw-search-input')
search_box_element.clear()
search_box_element.send_keys('biotechnology')
search_box_element.send_keys(Keys.ENTER)
search_box_element_two = browser.find_element_by_id('more-news-results')
search_box_element_two.click()
last_height = browser.execute_script("return document.body.scrollHeight")
browser.execute_script("window.scrollTo(0, document.body.scrollHeight);")
next_page_click_element = WebDriverWait(browser, 10).until(
EC.presence_of_element_located((By.XPATH, "//div[#id='more-news-pagination']/div/div/div/a[text()='Next']"))
)
next_page_click_element.click()
Let me know if this Answers your Question.

Resources