WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.co.in");
driver.findElement(By.xpath("//*[#id='gs_htif0']"))
.sendKeys("selenium");
I want to send to use xpath as a locater.
Error:
Exception in thread "main"
org.openqa.selenium.InvalidElementStateException: Element is disabled
and so may not be used for actions Command duration or timeout: 75
milliseconds
After that I want to get the google suggestions printed.
You can try using name instead of XPATH. Name selector is faster than Xpath.
driver.findElement(By.name("q"));
You are trying to access the google search bar by ID, try this id: //*[#id='lst-ib']
driver.findElement(By.xpath("//*[#id='gs_htif0']"))
.sendKeys("selenium");
You can also try these:
python: driver.find_element_by_xpath("//*[#name='q']").send_keys('selenium')
java: driver.findElement(By.xpath("//*[#name='q']")).sendKeys("selenium");
To get the suggestions you can try this:
List<WebElement> rows = driver.findElements(By.xpath("//*[#role='option']"));
for (int i=0; i<rows.size(); i++){
System.out.println(rows.get(i).getText());
}
Output: selenium selenium ide selenium tutorial selenium webdriver
First click in the input box and then use . sendKeys("whatever you want to type ")
Related
this is the screenshot of my first test case in selenium
pls help me with this what error its there as I am new to selenium
Try using something like this:
WebDriverWait wait = new WebDriverWait(driver,30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(text(),'COMPOSE')]")));
// click on the compose button as soon as the "compose" button is visible
driver.findElement(By.xpath("//div[contains(text(),'COMPOSE')]")).click();
}
Source: https://www.browserstack.com/guide/wait-commands-in-selenium-webdriver
Your error is caused by trying to Click on something that the webdriver has not found, giving it time to load the webpage will help.
I am trying to find an element on this page. Specifically the bid price in the first row: 196.20p.
I am using selenium and this is my code:
from selenium import webdriver
driver = webdriver.PhantomJS()
address = 'https://www.trustnet.com/factsheets/o/g6ia/ishares-global-property-securities-equity-index-uk'
xpath = '//*[#id="factsheet-tabs"]/fund-tabs/div/div/fund-tab[3]/div/unit-details/div/div/unit-information/div/table[2]/tbody/tr[3]/td[2]'
price = driver.find_element_by_xpath(asset['xpath'])
print price.text
driver.close()
When executed I receive the following error
NoSuchElementException: Message: {"errorMessage":"Unable to find element with xpath '//*[#id=\"factsheet-tabs\"]/fund-tabs/div/div/fund-tab[3]/div/unit-details/div/div/unit-information/div/table[2]/tbody/tr[3]/td[2]'","request":{"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"214","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:62727","User-Agent":"Python http auth"},"httpVersion":"1.1","method":"POST","post":"{\"using\": \"xpath\", \"sessionId\": \"8faaff70-af12-11e7-a17c-416247c75eb6\", \"value\": \"//*[#id=\\\"factsheet-tabs\\\"]/fund-tabs/div/div/fund-tab[3]/div/unit-details/div/div/unit-information/div/table[2]/tbody/tr[3]/td[2]\"}","url":"/element","urlParsed":{"anchor":"","query":"","file":"element","directory":"/","path":"/element","relative":"/element","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/element","queryKey":{},"chunks":["element"]},"urlOriginal":"/session/8faaff70-af12-11e7-a17c-416247c75eb6/element"}}
Screenshot: available via screen
I have used the same approach, but with different xpath, on yahoo finance and it works fine, but unfortunately the price I am looking for is not available there.
If I didn't fail to understand your requirement then this is the price you wanted to scrape. I used css selector here.
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://www.trustnet.com/factsheets/o/g6ia/ishares-global-property-securities-equity-index-uk')
price = driver.find_element_by_css_selector('[ng-if^="$ctrl.priceInformation.Mid"] td:nth-child(2)').text
print(price.split(" ")[0])
driver.quit()
Result:
196.20p/196.60p
If you wanna stick to xpath then try this:
price = driver.find_element_by_xpath('//*[contains(#ng-if,"$ctrl.priceInformation.Mid")]//td[2]').text
Expected [object Undefined] undefined to be a string,
The code I am using is following:
System.setProperty("webdriver.gecko.driver","E:\\Software\\geckodriver-
v0.16.1-win64\\geckodriver.exe");
WebDriver wd= new FirefoxDriver();
wd.get("https://www.google.co.in/");
//wd.findElement(By.xpath(".//*
[#id='gbw']/div/div/div[1]/div[1]/a")).click();
wd.findElement(By.linkText("Gmail")).click();
WebElement e1= wd.findElement(By.xpath("//input[#id='identifierId']"));
e1.sendKeys("abc#gmail.com");
wd.findElement(By.xpath("//div[#id='identifierNext']/content/span[text()='Ne
xt']")).click();
error log
error log
Exception in thread "main" java.lang.NoSuchMethodError: com.google.common.base.Preconditions.checkState(ZLjava/lang/String;Ljava/lang/Object;)V
at org.openqa.selenium.remote.service.DriverService.checkExecutable(DriverService.java:136)
at org.openqa.selenium.firefox.GeckoDriverService.access$000(GeckoDriverService.java:41)
at org.openqa.selenium.firefox.GeckoDriverService$Builder.usingFirefoxBinary(GeckoDriverService.java:108)
at org.openqa.selenium.firefox.FirefoxDriver.toExecutor(FirefoxDriver.java:204)
at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:108)
at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:104)
at register_prctc.gmail.main(gmail.java:15)
Here is the solution to your Question:
To work with Selenium 3.4.0, geckodriver v0.16.1 & latest Mozilla Firefox 53.x you need to set the absolute path of the geckodriver in your code as:
System.setProperty("webdriver.gecko.driver","C:\\your_dir\\geckodriver.exe");
As per best practices you should not use Thread.sleep(6000), instead use ImplicitlyWait or ExplicitWait.
The xpath .//[#id='gbw']/div/div/div[1]/div[1]/a you used doesn't identifies any unique element. To find the element Gmail link you can use the linkText locator as:
wd.findElement(By.linkText("Gmail")).click();
For sending text into Email or Phone field provide a unique xpath as:
WebElement e1= wd.findElement(By.xpath("//input[#id='identifierId']"));
The xpath to click on Next button looks vulnerable to me, you may like to change it to : wd.findElement(By.xpath("//div[#id='identifierNext']/content/span[text()='Next']")).click();
Here is the working set of your own code with some simple tweaks:
System.setProperty("webdriver.gecko.driver","C:\\geckodriver.exe");
WebDriver wd= new FirefoxDriver();
wd.get("https://www.google.co.in/");
wd.findElement(By.linkText("Gmail")).click();
WebElement e1= wd.findElement(By.xpath("//input[#id='identifierId']"));
e1.sendKeys("id#gmail.com");
wd.findElement(By.xpath("//div[#id='identifierNext']/content/span[text()='Next']")).click();
Let me know if this Answers your Question.
Remove Selenium-java-2.53.1.jar file and update all jars
Before moved to FF9/10, this line sendKeys can work well with FF8.0.1 + Selenium 2.18. But after upgrade FF to 9 and 10, sendKeys will get nothing in CKEditor. No exception, no warning. I am wondering if it is the bug of CKEditor? or FF 9/10? or WebDriver? Anyone have clue for this?
DesiredCapabilities desiredCapabilities = DesiredCapabilities.firefox();
desiredCapabilities.setPlatform(Platform.WINDOWS);
URL remoteAddress = new URL("http://127.0.0.1:4444/wd/hub");
RemoteWebDriver driver = new RemoteWebDriver(remoteAddress,desiredCapabilities);
driver.get("http://ckeditor.com/demo");
WebElement element = driver.findElement(By.id("cke_contents_editor1")).findElement(By.tagName("iframe"));
element.sendKeys("Cheese!");
Of course, I can access the instance of CKEditor directly like below, but it just a workaround.
((JavascriptExecutor) concorddriver).executeScript("CKEDITOR.instances.editor1.insertText( 'hello' );");
If sendKeys() doesn't work, try a click() before using it.
This often helped on similar issues.
But before you try this, check if the element is really found!
If not check if the element's id is still the same and/or try to use other By. methods (like By.className(),...)
I'm currently trying to improve my Selenium tests on IE. I would like to use javascript-xpath instead of ajaxslt.
My setUp function is :
public void setUp() throws Exception {
super.setUp(host, browser);
selenium.setSpeed(Constants.DELAY_BETWEEN_ACTIONS);
selenium.windowMaximize();
selenium.allowNativeXpath("false");
selenium.useXpathLibrary("javascript-xpath");
}
When I try to find an element by Xpath, for instance :
selenium.click("xpath=//a[#id='linkLogin']");
I get the error (only when i try to use javascript-xpath):
com.thoughtworks.selenium.SeleniumException: ERROR: Invalid xpath [2]: //a[#id='linkLogin']
What do i miss to correctly use javascript-xpath ?
Thanks for your help.
Have you considered changing your locator strategy away from xpath? I've been using a form of CSS locators that are on average 5 times faster than xpath.