.send_keys(Keys.RETURN) - python - selenium-webdriver

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)

Related

Selenium Webdriver - Keys not being sent to search boxes

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

OpenQA.Selenium.WebDriverException: [windowHandle] is not a top level window handle solution

Some people including me were suffering from this issue called "OpenQA.Selenium.WebDriverException: [windowHandle] is not a top level window handle".
There are lot of question asked and answered on "how to attach to a TopLevelWindow" but literally none is talking on "how to attach to a NonTopLevelWindow". I searched a lot for a solution but there was nothing on it. But after reading a code shared in this answer on GitHub, I realized what the solution is.
I was so disgusted after it because of the simplicity of the solution! Then I thought to share it with everyone.
The solution is so simple. As the new window which is a Child Node (i.e. resides under the main application tree), we can not attach to it by creating a new session. The reason is that, it is not a top level window and we can only attach to a top level window with this process explained on GitHub.
What we need to do is just simply search for the window by its name and you will get access of that window (same as finding an UI element)
For example- This code is for such a window which is a TopLevelWindow:
# create a desktop session to find the new window
desired_caps = {}
desired_caps["app"] = "Root"
newDriver = webdriver.Remote(WindowsApplicationDriverUrl, desired_caps)
# Find the NativeWIndowHandle of the new window
newWindow = newDriver.find_element_by_name("SmarTTY - New SSH Connection")
newWindowHandle = newWindow.get_attribute("NativeWindowHandle")
# create a new session to attach the new window with its NativeWindowHandle
desired_caps = {}
desired_caps["appTopLevelWindow"] = newWindowHandle
connWinDriver = webdriver.Remote(WindowsApplicationDriverUrl, desired_caps)
And this code is for such a window which is a NonTopLevelWindow:
# Find the new window
newPopUpWindow = driverMain.find_element_by_name("SmarTTY - New SSH Connection")
In this case the first code will not work due to the window is not being a top level window. But the second code will work.
This window (window name is SmarTTY - New SSH Connection) pops open after I click a button on the previous window. The application name on which this example is based on is SmarTTY. And the above codes are Python codes for WinAppDriver.
Here is my C# code, I am adding it as desired capabilities are depricated and options should be used instead:
AppiumOptions rootSessionOptions = new AppiumOptions();
rootSessionOptions.AddAdditionalCapability("app", "Root");
rootSessionOptions.AddAdditionalCapability("deviceName", "WindowsPC");
_driver = new WindowsDriver<WindowsElement>(new Uri("http://127.0.0.1:4723"), rootSessionOptions);
_driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
var VSWindow = _driver.FindElementByName("Your project name without .csproj - Microsoft Visual Studio");
var VSTopLevelWindowHandle = VSWindow.GetAttribute("NativeWindowHandle");
VSTopLevelWindowHandle = (int.Parse(VSTopLevelWindowHandle)).ToString("x");
AppiumOptions VisualStudioSessionOptions = new AppiumOptions();
VisualStudioSessionOptions.AddAdditionalCapability("appTopLevelWindow", VSTopLevelWindowHandle);
_driver = new WindowsDriver<WindowsElement>(new Uri("http://127.0.0.1:4723"), VisualStudioSessionOptions);
_driver.SwitchTo().Window(_driver.WindowHandles[0]);
# After login i am not able to perform any operation
#Conftest file code
import pytest
from appium import webdriver
#pytest.fixture(scope="class")
def setUp(request):
desired_caps = {}
desired_caps["app"] = r"C:\\Program Files (x86)\\Giesecke Devrient\\Compass VMS\\Compass VMS.exe"
driver = webdriver.Remote(command_executor='http://127.0.0.1:4723',
desired_capabilities= desired_caps)
#driver.implicitly_wait(10)
request.cls.driver = driver
yield
driver.close()
#Main Code
from appium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
from Utility.Base import BaseClass
# #pytest.mark.usefixtures("setUp")
class TestDe_Login(BaseClass):
def test_initialize(self):
self.driver.find_element_by_xpath("//*[#Name='Login']//*[#LocalizedControlType='text' "
"and #Name='User ID:']/..//*[#LocalizedControlType='edit']").send_keys("t2")
self.driver.find_element_by_accessibility_id("passwordTextBox").send_keys("1")
self.driver.find_element_by_name ("&Login").click()
wait = WebDriverWait(self.newDriver, 20)
wait.until(EC.presence_of_element_located((By.NAME, '&Logout'))).click()[enter image description here][1]

How to test the 'drag and drop' feature from the react-beautiful-dnd library using Selenium in Java?

I have tried various options, but have been unable to simulate a mouse click to drag an element from one position to another in a browser using Selenium. When the test runs, I see the element get selected, but it does not move to the specified drop point. Any suggestions or insights are greatly appreciated!
Here's how I defined the function in my latest attempt (variations on this theme also tried and failed):
private void dragAndDrop(WebElement dragPoint, WebElement dropPoint, WebDriver driver) {
Actions builder = new Actions(driver);
builder.clickAndHold(dragPoint).perform();
builder.pause(Duration.ofSeconds(1));
builder.moveByOffset(10,0).perform();
builder.moveToElement(dropPoint).perform();
builder.moveByOffset(10,0).perform();
builder.pause(Duration.ofSeconds(1));
builder.release();
builder.build();
builder.perform();
}
Also tried the following (same result):
private void dragAndDrop(WebElement dragPoint, WebElement dropPoint, WebDriver driver) {
Actions builder = new Actions(driver);
Action dragAndDrop = builder.dragAndDrop(dragPoint, dropPoint).build();
dragAndDrop.perform();
}
In the test, the 2 elements are identified uniquely using xpath and the function is called:
WebElement dragPoint = driver.findElement(By.xpath(".../div[3]/...(etc.)/div[#class='rst__moveHandle']"));
WebElement dropPoint = driver.findElement(By.xpath(".../div[5]/...(etc.)/div[#class='rst__moveHandle']"));
dragAndDrop(dragPoint, dropPoint, driver);
Relevant libraries:
react-beautiful-dnd: https://github.com/atlassian/react-beautiful-dnd
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;

verify search working proprly using Selenium

I am beginner for Selenium, just I want to check that search of the website working properly. Means when I enter any keyword in the search box and click on search is it provides correct search result. How to Check using Selenium WebDriver. Please guide me.
You should provide more details about your query
for example you should include the URL your are working on or a similar URL for the one you are working on
You can use an item property from the search result page as an assertion point for your test
Try below code
package StackOverFlow.StackOverFlow;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.Test;
public class App
{
#Test
public void test () throws InterruptedException{
System.setProperty("webdriver.chrome.driver", "C:\\Chrome\\2\\chromedriver.exe");
WebDriver Dr = new ChromeDriver();
Dr.manage().window().maximize();
Dr.get("http://www.conns.com");
Thread.sleep(4000);
WebElement ele = Dr.findElement(By.xpath(".//*[#id='search']"));
ele.sendKeys("furniture");
ele.sendKeys(Keys.ENTER);
Thread.sleep(4000);
String result= Dr.findElement(By.xpath("/html/body/div[3]/div/div[4]/div[2]/div/div[1]/h1")).getText();
Assert.assertEquals("Furniture & Mattresses", result);
}
}
I used a very obvious element (Furniture & Mattresses) on the page to assert against its text

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