How take XPATH inside a iframe? - selenium

Im trying to get the XPATH inside a iframe called "gadget_6"
i need the xpath of select div. Using selenium i do:
self.change_frame('__gadget_6') #Change to the correct iframe
after that, i try to do:
self.selec_orgvdc = self.driver.find_element(By.XPATH,'/html/body/div[5]/div/div/div/table/tbody/tr/td/div')
for posteriorly do:
self.selec_orgvdc.click
but i obtain the error:
InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath expression /html/body/div[5]/div/div/div/table/tbody/tr/td/div because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '/html/body/div[5]/div/div/div/table/tbody/tr/td/div' is not a valid XPath expression.
Someone can help me?
New modification:
self.change_frame('__gadget_6')
time.sleep(4)
self.logger.info("Buscando orgvdc")
#self.selec_orgvdc = self.driver.find_element(By.CSS_SELECTOR,'div.GN4Y2ATIMD table div.gwt-Label')
self.selec_orgvdc = self.driver.find_element(By.XPATH,'//div[5]/div/div/div/table/tbody/tr/td/div')
self.logger.info("Encontrado!")

Hi to work with xpath inside iframe first you have to sitch your driver instance ti that iframe then only you can perform any action
1.To switch to an iframe plz do it like : driver.switchTo().frame("selfservice");
Also looking at your source code i can clearly see there is 2 (two) i frames
1.iframe id = __gadget_6
2.iframe id = selfservice
hence i can guess why its not working in your case cause you are switching to a wrong i frame
now try this xpath //*[#class='GN4Y2ATIMD']/div/div/div/table/tbody/tr/td/div to perform click this will surely work.

XPath shouldn't include the /html/body. try this:
self.selec_orgvdc = self.driver.find_element(By.XPATH,'//div[5]/div/div/div/table/tbody/tr/td/div')
Note that this will be quite brittle. Even better would be to do something like this CSS:
self.selec_orgvdc = self.driver.find_element(By.CSS,'div.GN4Y2ATIMD table div.gwt-Label')

Related

Unable to locate an element using and condition in xpath

When use the condition in Console it says the condition is true but when I run the script, I'm getting the unable to find the element.
Below is the HTML:
<div class="ipc-button__text">Sign In</div>
And the XPath I'm trying to use:
(//div[#class='ipc-button__text']) and (//div[contains(text(),'Sign')])
Try combining the expressions.
//div[#class='ipc-button__text' and contains(text(),'Sign')]
This should fix the issue.
Otherwise each //div expression would be evaluated separately and result in the whole expression being true (because both //div[#class='ipc-button__text'] and //div[contains(text(),'Sign')] are true somewhere in the document), but without a specific div element selected matching both sub-expressions.
Please use following xpath ---- //*[contains(text(),"Sign In")]
There might be the reason that XPath of the element is not loaded when we trying to perform action instead of doing simple use explicit wait condition before performing any action try to use the below approach.
// xpath = //*[contains(text(),"Sign In")]
WebDriverWait wait = new WebDriverWait(driver, timeInSeconds);
WebElement element
= wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(xpath)));
// Perform any action
element.click();

How to implement //*[contains(text(), '"example"')] to construct an Xpath for dynamic contents for Selenium Webdriver:?

I'm working through Selenium Webdriver, and I've come up to the issue of dynamic objects as a DOM ID. One of my instances gets generated as an ID, something like this:
"//*[#id="888itsAnExampleBoi573"]/div[1]/div[2]",
and I need to click on the button in the example item to Make Stuff Happen.
Because I cannot predict what an objectID will be for my dynamic content, however, I would like to be able to do this, instead:
//*[contains(text(), 'example')]/div[1]/div[2].
I've tried to do this, but I'm returned a strange error:
Caused by: org.openqa.selenium.InvalidSelectorException: invalid
selector: Unable to locate an element with the xpath expression
//*[contains(text(), 'example')]/div/div/div[1]/div[3]/div
because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document':
The string '//*[contains(text(), 'example')]/div/div/div[1]/div[3]/div'
is not a valid XPath expression.
On a different element that is a hyperlink with text elements, I've been able to use contains(text()) to solve things, so I believe I've formatted this correctly.
I've tried a few different things to solve this issue, but am at somewhat of a loss as to how to solve this. Does anyone have any ideas or resources to point me towards? Or better yet, a solution?
This error message...
Caused by: org.openqa.selenium.InvalidSelectorException: invalid selector: Unable to locate an element with the xpath expression //*[contains(text(), 'example')]/div/div/div[1]/div[3]/div because of the following error: SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//*[contains(text(), 'example')]/div/div/div[1]/div[3]/div' is not a valid XPath expression.
...implies that the Locator Strategy is not a valid XPath expression.
The expressions:
'//*[contains(text(), 'example')]/div/div/div[1]/div[3]/div'
"//*[contains(text(), "example")]/div/div/div[1]/div[3]/div"
Are invalid as '' and "" were present in multiple places within the xpath.
Solution
As an effective alternative dynamic xpath for the following:
"//*[#id="888itsAnExampleBoi573"]/div[1]/div[2]"
will be:
"//*[contains(#id,'itsAnExampleBoi')]/div[1]/div[2]"
It appears that in order to solve this, sometimes you must frame the question. As I had been using a relative XPATH, I had been working under the pretense that the text div itself was where I would be able to access elements that were not the children of my div. They were actually only loosely related!
"//*[contains(text(), 'Flag this')]
//parent::div
//parent::div[contains(#class,'exampleDrop')]
//child::div[contains(#class, 'pointed exampleAction')]"
I ended up installing Chropath and spidering around the DOM, finding the div with the text in it, and using a relative path in this statement. If you're having issues like these, sometimes I would recommend making sure that you're asking the right question.

SyntaxError: The expression is not a legal expression

I don't know how I can use any elements to get clicked on this submit button.
I've tried it using xpath.,
web.find_elements_by_xpath('\\input value="submit" type="submit"').click()
but I get the error metioned in the title.
I searched and I couldn't find any solution.
Here's the inspected element by the browser:
What you pass to find_elements_by_xpath() doesn't looks like XPath. Also find_elements_by_xpath() should return you a list which doesn't have an attribute click(). Try to use below instead:
web.find_element_by_xpath('//input[#value="submit" and #type="submit"]').click()

Getting org.openqa.selenium.InvalidSelectorException only for Chrome

I am tring to find a button in a webpage using find elements, the page can contain one of the below button ID's.
driver.findElements(By.xpath("//*[contains(#id,'topBtn')]"))
driver.findElements(By.xpath("//*[contains(#id,'WSMplasticTop')]"))
driver.findElements(By.xpath("//*[contains(#id,'bottomApplyBtn')]"))
The above code is working as expected when i use the Firefox Driver, where as getting the below error when i run in Chrome Driver.
org.openqa.selenium.InvalidSelectorException: invalid selector: Unable to locate an element with the xpath expression //*[contains(#id='bottomApplyBtn')] because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//*[contains(#id='bottomApplyBtn')]' is not a valid XPath expression.
Just wanted to know whether i have done any mistake
Try to use
'//*[contains(#id,'bottomApplyBtn')]'
instead of
'//*[contains(#id='bottomApplyBtn')]'
When you are using contains method in your XPath expression, you basically are using an inbuilt method to find the element.
This method takes 2 parameters.
First the tag you in which you want to search.
Second is the actual text value which you are looking for inside the above tag.
Basically you have to call the method with 2 parameters and that 2 parameters should be comma separated.
So //*[contains(#id='bottomApplyBtn')] is wrong you should instead remove this = sign.
//*[contains(#id, 'bottomApplyBtn')]
|_______|____________________ Parameter 1
|__________________________________Parameter 2
Hope it helps!

Selenium Webdriver: cssselector

I am trying to do SignIn for this. In it click on 'SignIn' which I have done it successfully. Now when trying to type in Username/Password using Xpath it shows exception which says
Exception in thread "main"
org.openqa.selenium.ElementNotVisibleException: element not visible
code is :-
driver.findElement(By.xpath(".//*[#id='load_form']/fieldset[1]/input")).sendKeys("test123");
driver.findElement(By.xpath(".//*[#id='load_form']/fieldset[2]/input")).sendKeys("test123");
I know this Xpath is same as used in SignUp form, so what are the other ways to locate these two fields? How we can use it by using cssselector?
Thanks for the help in advance.
Go One Level up on finding the relative xpath with
//div[#id='login']/form[#id='load_form']//input[#name='username']
//div[#id='login']/form[#id='load_form']//input[#name='password']
Try this
//For Username
driver.findElement(By.xpath("(//input[#name='username'])[2]")).sendKeys("username test");
//or
driver.findElement(By.cssSelector("#login > #load_form > fieldset > input[name='usernam']")).click();
//For password
driver.findElement(By.xpath("(//input[#name='password'])[2]")).sendKeys("password test");
//or
driver.findElement(By.cssSelector("#login > #load_form > fieldset > input[name='password']")).click();
the above codes are working for me.
There are basically two elements found by provided xpath, and it works with first found element which is invisible as your exception saying, you should try using cssSelector as below :-
driver.findElement(By.cssSelector("div#login input[name = 'username']")).sendKeys("test123");
driver.findElement(By.cssSelector("div#login input[name = 'password']")).sendKeys("test123");
Note:- For learning about cssSelector follow this url and for xPath follow this url
I'm suggesting you before selenium execution with the locator you need to sure that using locator is correct and returns single or multiple element at your browser console by pressing f12. for verify xPath you should use $x("your xpath expression") and for cssSelector you should use document.querySelector("your css selector expression")..
Hope it will help you..:)