I am practicing Selenium using Java Eclipse. I want to be able to log into Twitter using Selenium. I don't see any element associated with the username and password fields though. Why is that are no elements? How can I access these fields?
Thanks!
You can use xpath as below. These expressions are working form me.
WebDriver driver = new FirefoxDriver();
driver.get("https://twitter.com/login");
driver.findElement(By.xpath("//div[#class='signin-wrapper']//input[#name='session[username_or_email]']")).sendKeys("Yourusername");
driver.findElement(By.xpath("//div[#class='signin-wrapper']//input[#name='session[password]']")).sendKeys("YourPassword");
driver.findElement(By.xpath("//button[contains(text(), 'Log in')]")).click();
You can simply access those fields with XPath:
//input[#placeholder="Phone, email or username"]
//input[#placeholder="Password"]
Let me know if for some reason you cannot use these selectors or in case of any exceptions ocurred
Here's how to find the XPath:
In chrome, click Inspect on the element of interest. You will have the option of copying the XPath from there. Image attached for reference:
Please use the xpath as below. we can able to access that.
(//*[#name='session[username_or_email]'])[2]
Related
I want to click on login button. Which attribute should I use to click on login button? I am new to selenium web driver. I am unable to find its link text, id, class name, name. I am not able to find its XPath or CSS selector. please advice me with the code. Image is here
You can use either Xpath which is
.//*[#id='main-menu']/ul/li[8]/a
or CSS Selector which is
.login
To click on element with text as LOGIN within the url https://phptravels.com/404/ you can use either of the Locator Strategies:
CssSelector:
"a.login[href='http://phptravels.org']>span"
XPath:
"//a[#class='login' and #href='http://phptravels.org']/span[contains(.,'LOGIN')]"
//or
"//a[#class='login' and #href='http://phptravels.org']/span[contains(.,'Login')]"
You have multiple options, just avoid using xpath since its an overkill
li.user-login>a.login>span
or
a.login>span
or xpath
.//a[#class='login']/span[text()='Login']
The html element Im trying to locate is the "Shared" link.
I wrote a dynamic xpath to locate the element and it shows as the element was identified in the developer console.
But when i use the xpath that i wrote in the developer console to locate the element using selenium, it does not locate the element.
The method i used to check if it locates the element is shown below.
I could not figure out why this issue occurs, Is it because of a issue in the xpath that i have written or because of another issue?
Code you can try out is :
new WebDriverWait(driver,10).until(ExpectedConditions.elementToBeClickable(By.xpath(" your Xpath ")));
driver.findElement(By.xpath("your Xpath")).click();
The Xpath you have written would work if only one title is present on current page.
driver.find_elements doesn't have attribute to click() so use driver.findElement instead of Elements
I am trying to explore log In button xpath with this site https://www.componence.com/login, by just recording and play back.Then I tried to get it through firepath and chrome browser default xpath copier.
But it looks like every time submit button xpath get changed with page load. I got following xpath for "Sign IN" button.
.//*[#id='yui_patched_v3_11_0_1_1487250469606_202']
.//*[#id='yui_patched_v3_11_0_1_1487251369606_202']
.//*[#id='yui_patched_v3_11_0_1_1487250229606_202']
.//*[#id='yui_patched_v3_11_0_1_1487254369606_202']
Can you please help me to retrieve correct xpath of Sign IN button which I can use with selenium IDE?
You can use below XPath to handle dynamic id:
//button[starts-with(#id, "yui_patched_v3_11_0_1_")]
But better solution is to use text content of element:
//button[normalize-space(text())="Sign In"]
I will have to disagree with the second statement of #Andersson, since it will work for .com but not for .nl.
As I see the site has a second language and my opinion is to avoid using selectors based on text on a multi-language environment.
Also as I see the id seems does not have a meaningful value, in this case try to identify a unique parent section and go from there.
One option for css/xpath would be:
css: form.sign-in-form button
xpath: //form[contains(#class, 'sign-in-form')]//button
please help me.
Actually I want to find element locator in selenium using XPath since the id is auto-generate (the ID always changed when the page refresh). but the XPath always changed too. here is the XPath locator :
html/body/div[4]/div/div[1]/div/table[1]/tbody[2]/tr[1]/td/div/nobr
html/body/div[4]/div/div[1]/div/table[1]/tbody[2]/tr[1]/td/div/nobr
html/body/div[15]/div/div[1]/div/table[1]/tbody[2]/tr[1]/td/div/nobr
html/body/div[7]/div/div[1]/div/table[1]/tbody[2]/tr[1]/td/div/nobr
Actually I already try to use :
html/body/div[class='scrollingMenu']/div/div[1]/div/table[1]/tbody[2]/tr[1]/td/div/nobr
the div itself has unique classname scrollingMenu. but it is not working. it always give the error element not found.
You can use element's id in your XPath as follow:
Suppose id="constantPart-generatedPart12345", then XPath is
//*[contains(#id, "constantPart-")]
PS. If this not works, update your question with HTML for target element, so I can edit XPath appropriatelly
Open the page in Google Chrome and use F12 to test your xPath before implementing it - has saved me a lot of time
I'd like to be able to test the content in CKeditor using webdriver.
However there are some hurdles; firstly CKeditor uses Iframes, and there are several Iframes on the page, so not sure how to switch reliably to it using WebDriver as they don't have specific names.
In addition, the content inside the editor is within a <body></body> tag inside the iframe. I'm not sure how to get WebDriver to return the content reliably.
Has anyone actually tried to do this in their tests? If so, how did you achieve it?
Thanks.
You can use the CKEditor api and execute javascript. Not sure which selenium driver you are using but here is the java script you can use to get the HTML for:
"return CKEDITOR.instances['youreditoridhere'].getData();"
u may refer this
http://bharath-marrivada.blogspot.com/2012/03/fckeditor-switch-activeelement.html
hopes this help ;D