Selenium Webdriver - Keys not being sent to search boxes - browser-automation

I am starting to use Selenium Webdriver (Chrome) and while pages open automatically keys are not being sent for some reason. For example, I open google.com, and I use a "BY.ID" to aim at the search box by it's ID and send "Hi" but for some reason, it doesn't send the keys, I tried looking at the Selenium documentation but It kinda left me more confused, anybody knows why keys aren't being sent? Thanks in advance!
from selenium import webdriver
from selenium.webdriver.common.by import By
browser = webdriver.Chrome('/Users/thras/Desktop/Chromedriver/chromedriver')
browser.get('https://www.youtube.com')
title = browser.title
browser.implicitly_wait(3)
text_box = browser.find_element(By.ID,"search").send_keys('Hi')

from selenium import webdriver
from selenium.webdriver.common.by import By
browser = webdriver.Chrome('/Users/thras/Desktop/Chromedriver/chromedriver')
browser.get('https://www.google.com')
title = browser.title
browser.implicitly_wait(3)
text_box = browser.find_element(By.NAME, value="q")
text_box.send_keys('Hiii')
fixed, it was syntax issue it seems, I needed to add "value" before the actual ID

Related

.send_keys(Keys.RETURN) - python

Not sure if I'm in the right section here,But I need a little help trying to make my code do a keyboard "ENTER"
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.get('https://www.adidas.com/us')
searchbox = driver.find_element_by_xpath('//*[#id="app"]/div/div/div/div/div/div[1]/div/div[3]/div/div[2]/div[1]/div/form/input[1]')
searchbox.send_keys('NMD_R1 SHOES').send_keys(Keys.ENTER)
It should:
searchbox.send_keys('NMD_R1 SHOES' + Keys.ENTER)
I suggest to locate searchbox use .find_element_by_name('q'), although your way also works.
Finally:
searchbox = driver.find_element_by_name('q')
searchbox.send_keys('NMD_R1 SHOES' + Keys.ENTER)

selenium IDE is successfully sending keys to hidden input box, but webdriver is throwing an error as element not visible

I have an input box which is not visible, Selenium IDE in record and playback mode is able to send keys into it and out put is successful.
same thing webdriver is throwing an error element is not visible therefore cannot be interacted with.
I have tried scripting using document.findElements.ByclassName.. there is no error but there is no output as well.
pls see the code below:
{
driver.findElement(By.cssSelector("li.cwd-clue")).click();
assertTrue(isElementPresent(By.xpath("//input[#class='cwd_input']")));
System.out.println("assert true");
WebElement tmpElement= driver.findElement(By.className("cwd_input"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("document.getElementsByClassName('cwd_input') [0].click();",tmpElement);
tmpElement.sendKeys("TELLER");}
Add WebDriverWait as shown.
WebElement ele=driver.findElement(By.xpath("//input[#class='cwd_input']");
WebDriverWait wait=new WebDriverWait(driver,10).until(ExpectedConditions.presenceOfElementLocated(ele));

Selenium hangs in loop

I'm using Selenium's Python bindings for a quick scraping job, but have found that for whatever reason, the Firefox WebDriver becomes unresponsive after precisely nine iterations.
Basic operation consists of loading a page, selecting a state from a dropdown menu of all fifty, clicking through to the results page and then returning to select another state. Irrespective of where I start in the list of states, after iterating through nine pages, the Firefox WebDriver becomes unresponsive, though no errors are thrown.
Code in question below:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
def set_up(url):
driver = webdriver.Firefox()
driver.get(url)
return(driver)
def search(driver):
for i in range(1, 49):
element = driver.find_element_by_id("StateList")
all_options = element.find_elements_by_tag_name("option")
print("Acquiring data for" + str(all_options[i].get_attribute("label")))
all_options[i].click()
driver.find_element_by_id("SearchButton").click()
#scrape page components here
driver.back()
url = 'http://www.example.com/'
driver = set_up(url)
search(driver)
Any thoughts?

Avoid opening browser on remote server during selenium call

I have written a selenium application using webdriver. I wish to run it on a remote server. When I do that by logging into the server via putty (along with Xming), the selenium tries opening the browser on the server only and load the pages through the external display. However in doing that, it takes a lot of time than if I would have been able to get the browser open on my localhost only (and not the server). Is it possible for such thing to happen or opening on the server only is the only option (which is painfully slow). Kindly tell me if I am missing something as well.
Thanks in advance.
Try using Selenium Grid, instead of Putty, to run your Selenium application on a remote server. The Selenium website has an excellent Quick Start guide for using the Selenium Grid: http://code.google.com/p/selenium/wiki/Grid2.
You can run Selenium with a"headless" driver, HtmlUnitDriver, that does not actually open a browser:
http://code.google.com/p/selenium/wiki/HtmlUnitDriver
Note: HtmlUnitDriver will accept an argument, so that it can emulate a specific driver.
#Lori
I implemented the code but it still tries opening it from putty so takes a lot of time to get the work done. The code is as follows: 'code'
import sys
from scrapy.spider import BaseSpider
from scrapy.http import FormRequest
from scrapy.selector import HtmlXPathSelector
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.selector import HtmlXPathSelector
from scrapy.item import Item
from scrapy.http import Request
from selenium import selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
class DmozSpider(BaseSpider):
name = "linkedin_crawler"
#defence news
global company
global query
companyFilename = '<filename>'
f=open(companyFilename,"r")
f.seek(0)
company = f.readline().strip()
f.close()
queryFilename = '/var/www/Symantec/recon/recon/' +company+ '/Spider/LinkedIn/query.txt'
f = open(queryFilename)
f.seek(0)
query=f.readline().strip()
f.close()
start_urls = ['https://www.linkedin.com/uas/login'];
def __init__(self):
BaseSpider.__init__(self)
capabilities = webdriver.DesiredCapabilities()
self.selenium = webdriver.Remote(command_executor = 'http://localhost:5000/wd/hub', desired_capabilities = capabilities.FIREFOX)
def __del__(self):
self.selenium.quit()
def parse(self, response):
sel= self.selenium
sel.get(response.url)
global query
elem1 = sel.find_element_by_name("session_key")
elem2 = sel.find_element_by_name("session_password")
elem1.send_keys("myemailid")
elem2.send_keys("mypassword")
elem2.send_keys(Keys.RETURN)
return Request(query, callback=self.page_parse)
def page_parse(self,response):
global query
global company
sel= self.selenium
sel.get(query)
for i in xrange(10):
#for i in xrange(5):
nameFilename = ''
#print hxs
nlist = sel.find_elements_by_xpath('//ol[#class="search-results"]/li/div/h3/a')
fh = open(nameFilename,"a")
for j in xrange(len(nlist)):
url = nlist[j].get_attribute("href").encode('utf-8')
name = nlist[j].text.encode('utf-8')
fh.write(name)
fh.write("<next>")
fh.write(url)
fh.write('\n')
fh.close()
next = sel.find_elements_by_xpath('//a[#class="page-link"]')
next[0].click()
time.sleep(5)
To tun this script on server, I am using putty to fire the command. But then it again uses Xming to open the browser which makes the process slow again. So, how to run the script without opening the browser on my local machine via Xming so that this does not become the bottleneck. Thanks

How do I get Selenium 2 webdriver to work with Nightly (Firefox 64 bit)

I'm looking to execute a Selenium 2 test against the Nightly browser (FireFox 64bit). It records just fine with the Selenium IDE (v1.8.1). And it also plays back just fine using using the IDE. I then export the code to TestNG format. By the way I've loaded up the Webdriver Backed plugin so it exports WebDriver code for the Selenium 2 version. The problem I'm having is that when I export the code to TestNG format (Java) and execute it, the asserts never find the text on the screen. It executes fine so its not that the code didn't convert. It just seems to be something with the asserts. If I play it from the IDE plugin it finds it the text and asserts just fine, however as soon as it executes in Java it fails all the assertions. Any ideas to what might be going on. My code is below. Thanks much!
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverBackedSelenium;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import static junit.framework.Assert.*;
import com.thoughtworks.selenium.Selenium;
public class TestWithConfig {
WebDriver driver;
Selenium selenium;
#BeforeMethod
public void startSelenium() {
driver = new FirefoxDriver();
selenium = new WebDriverBackedSelenium(driver,
"http://en.wikipedia.org/wiki/Main_Page");
}
#AfterMethod
public void stopSelenium() {
driver.close();
}
#Test
public void testTest() {
selenium.setSpeed("600");
selenium.open("/wiki/Main_Page");
assertTrue("face not found",selenium.isTextPresent("face"));
selenium.click("link=Contents");
selenium.waitForPageToLoad("30000");
assertTrue("Below not found",selenium.isTextPresent("Below"));
selenium.click("link=Toolbox");
selenium.click("link=What links here");
selenium.waitForPageToLoad("30000");
assertTrue("Pages not found",selenium.isTextPresent("Pages that link to"));
selenium.click("link=exact:Talk:Wine");
selenium.waitForPageToLoad("30000");
assertTrue("Some not found",selenium.isTextPresent("Some"));
}
}
Since your using selenium 2 and webdriver, Assert's work a little different. I can see that you using the WebDriverBackedSelenium. However keep in mind. That's not selenium2. That is just a way to ease into selenium 2. I would use something like this.
WebElement tooltip = driver.findElement(By.xpath("the xpath of the element"));
assertNotNull("Name:","IP Address:",tooltip);
What I'm doing here is. i'm looking for a tooltip.inside that tooltip, there are two main labels that stay the same: Name and IP Address:. So I'm testing to see if those words exists or not in the tool tip. The output should be Name: IP Address:. That tells me the answer is true.

Resources