Get child element from complicated xpath - selenium

So I have some complicated situation in the DOM where I need to create XPath and get its child element. I have the following XPath
((//p[contains(text(),'" + read_contract_title() + "')]/parent::div/parent::div)[2]//button)[2]
The above is basically a ... more options button, that I need to click and then click the child element inside of it.
How do I get about getting the child element (button) inside the above XPath?
Looking forward to your help and reply.

In case parent element can be located with this XPath
"((//p[contains(text(),'" + read_contract_title() + "')]/parent::div/parent::div)[2]//button)[2]"
The button element inside it could be simply located with
"((//p[contains(text(),'" + read_contract_title() + "')]/parent::div/parent::div)[2]//button)[2]//button"
In case this is the only button element inside that parent element

Related

How to access 2nd element with same class name using css selectors

I want to access the 2nd element with same class name using css selectors.
1st element:
<a class="good">
2nd element:
<a class="good">
Css selector I am using :
a.good
but this accessing both of them.
How to access the 2nd one or anyone individually?
you can use pseudo selector, pseudo selector matches elements based on their position among a group of siblings.
check example link below
click here

Selenium Python css selector by class doesn't work

I have 4 ways to locate some element and want to click on it:
DOM is:
<div class="ui dropdown selection" tabindex="0">
And I locate this element by four ways:
(By.XPATH, "//div[#class='ui dropdown selection']")
(By.CSS_SELECTOR, "[class='ui dropdown selection']")
(By.CSS_SELECTOR, ".ui dropdown selection")
(By.CLASS_NAME, "ui dropdown selection")
I i just clik on element
Way 1 and 2 work, test is ok - and len(element) is 1
Way 3 and 4 don't work: NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".ui dropdown selection"} - and len(element) is 0
(Waits don't help, and Way 1, Way 2 don't require waits at all)
Could you tell me why Way 3 and Way 4 failed ?
When using multiple class names for the same tag within a CSS Selector, they must be separated by a dot instead of a space. Here's the correct way to express your third one:
(By.CSS_SELECTOR, ".ui.dropdown.selection")
OR
(By.CSS_SELECTOR, "div.ui.dropdown.selection").
As for the fourth one, you can't use By.CLASS_NAME with multiple class name components. You would have to pick one, but since that probably won't give you a unique selector, you'll be better off using one of the other ways to form a selector.
#Michael
(By.CSS_SELECTOR, ".ui.dropdown.selection")
(By.CSS_SELECTOR, "div.ui.dropdown.selection")
Both don't work - selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element
But notice that there is only 1 element because this works:
(By.XPATH, "//div[#class='ui dropdown selection']")
and len(element) = 1

Get innerHTML of a tag robotframework

I am new to robotframework. I have a requirement where I need the innerHTML of a tag. I tried something like this
Wait Until Page Contains Element xpath: //div[#id="toast-container"]
${temp_elem} = Set Variable xpath: //div[#id="toast-container"]
Log ${temp_elem}
But this is not working. Please help
What you need is to use the keyword Get Element Attribute, passing the locator to the element, and the target attribute:
${inner html}= Get Element Attribute xpath://div[#id="toast-container"] innerHTML

Selenium WebDriver - Find Element

I've gone through the Selenium Documentation for locating elements, but I can't seem to figure out how to find the element in my code.
Here is my code from my .cshtml:
<a onclick="alter('#key', '#value')" href="#edit" id="#key-display">#value</a>
I am trying to locate and click the #value at the end.
Here is what it looks like when I inspect the value on Chrome:
<a onclick="alter('February 9, 2018', '1.00000')" href="#edit" id="February 9, 2018-display">1.00000 gallons</a>
I am able to locate the element by link text like this:
chromeDriver.FindElementByLinkText("1.00000 gallons").Click();
However, the link text will change constantly and I want to be able to locate it after it changes.
I have tried locating by several ways:
chromeDriver.FindElementByLinkText("#value").Click();
chromeDriver.FindElementByXPath("//a[#id='#key-display']").Click();
chromeDriver.FindElementById("#key-display").Click()
You will have to locate the element by the HTML in the page after it's rendered so the cshtml variable name can't be used. Having said that, you should be able to find a locator that will work. I would start with a CSS selector like
a[href='#edit']
That should work unless you have multiple edit links on the page. If that doesn't work, I would try
a[href='#edit'][id$='-display']
To find the element and invoke click() on the element you can use either of the following Locator Strategies :
xpath (where ID contains -display and href is #edit)
"//a[contains(#id,'-display') and #href='#edit']"
You can be more granular adding the onclick attribute as :
"//a[contains(#id,'-display') and #href='#edit' and starts-with(#onclick,'alter')]"
cssSelector (where ID ends with -display and href is #edit)
"//a[id$='-display'][href='#edit']"
You can be more granular adding the onclick attribute as :
"//a[id$='-display'][href='#edit'][onclick^='alter']"

Selenium. child xpath selector as as pointer to parent element

So let's say my html structure have couple of similar div elements.
body/div/...
body/div/...
body/div/...
body/DIV/div[#class='class']
I would like to access the last one. But the one that is upper-cased.
So "//body/div/" selector will find many, but not related elements.
And "//div/div[#class='class']" will select the child div, not the upper case parent.
Use parent to select the parent of a given element:
//div/div[#class='class']/parent
If you want to be more verbose, you can also select a parent by a given tag name:
//div/div[#class='class']/parent::div