selenium python how to find and click element that change everytime - selenium

im trying to find an element with dinamic values , for example <span class="ms-Button-label label-175" id="id__177">Save</span> in inspect element, the id and class values tend to change for every refresh, how can i in this case find the element in selenium? i tried troguht xpath but seems doesnt work because can not find the path, i was thinking to find "Save" world torught always find by xpath but actually i dont know if im doing well : driver.find_element_by_xpath(//span(#.... but then? how can insert element if it changes everytime? thanks!

Something like this may work:
driver.find_element_by_xpath('//span[text()="Save"]')
But this will fail, if there is more than one button with text "Save" on the page.
In that case you may try to find some specific outer element (div, form, etc.) which does not change and contains the button. Then find the button inside of it.
With few requests with driver:
specific_div = driver.find_element_by_id("my_specific_div")
button = specific_div.find_element_by_tag_name("span") # e.g. there is only one span in that div
Or with more specific xpath:
button = driver.find_element_by_xpath('//div[#class="some-specific-class"]/span[text()="Save"]')
If needed, search for more nested elements before the button, so you can get more narrow search field.
More examples in the docs.

Related

How can I check that a dropdown field is disabled using selenium?

I am trying to write a function in selenium to check if a Reasons dropdown is showing as disabled, but can't quite get the xpath right. The code for the dropdown is in the pic, the function I'm working on is the second one (InputDisabled), having based it on the working first one (SearchDisabled):
` public By SearchDisabled(string searchId) => By.XPath($"//div[#id='{searchId}']//div[contains(#class, 'v-input--is-disabled')]");
public By InputDisabled(string inputId) => By.XPath($"//div[#id='{inputId}']//div[contains(#class, 'v-input--is-disabled')]");`
The inputId going into it is 'ai-confirm-allergy-modal-reason'. I've tried it as 'input[contains...' and 'contains(#disabled, 'disabled'...' among other things, but my xpath knowledge isn't great yet!
dropdown code
Use below code
String value = driver.findElement(By.XPath("//input[contains(#id, 'ai-confirm-allergy')]").getAttribute("disabled");
Assert.AssertEquals(value, "disabled");
I do not quite get your question.
well if you are trying to use xpath to locate an element, you can just use the id; assuming that it is unique.so:
driver.findElement(By.xpath("//input[contains(#id, 'ai-confirm-allergy')]")
should locate the webelement.
However, your xpath for the SearchDisabled is locating a div containing class 'v-input--is-disabled' with in another div with id of '{searchId}';
the same logic goes for the next one. your xpath is trying to locate a div containing class 'v-input--is-disabled' which is located with in another div located using input id. I don't think this combination can locate the element highlighted in the picture.

select element with changing xpath

Example Image
say I wanted to select the element with class=kbkey button red_selected sel. Its xpath from the example in the pic would be //*[#id="virtualKeysWrapper"]/div[3], so I have the following code:
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="virtualKeysWrapper"]/div[3]'))).click()
However, the div position of this element would change everytime I refresh the site. Was wondering what should I do to successfully select the element with class=kbkey button red_selected sel successfully everytime?
Avoid using index position in XPath if at all possible for this very reason. Without knowing exactly what the rest of the DOM looks like, my best guess is that you could use the following expression:
//div[#id='virtualKeysWrapper']/div[#class='kbkey button red_selected sel']
Alternatively, you could use
//div[#id='virtualKeysWrapper']/div[#sel='true']

How to get the clicked element in selenium when we don't know what are we clicking

I am clicking with the help of following lione oc code->
actions.moveToElement(objDriver.findElement(By.id("id_popcode")),coordinates.getX(),coordinates1.getY()-1).doubleClick().build().perform();
Basically i double click at a position(x,y) in our application. Individually we cannot click that particular element bcoz it has to be clicked at particular (x,y) itself. So i want to get the properties of that clicked element(which i click using actions command which i mentioned above) liked id, classname. Can some one help me with this...kinda stuck here..........
edit:
try execute.elementFromPoint() with JavascriptExecutor to get element by coordinates
JavascriptExecutor js = (JavascriptExecutor)driver;
WebElement theElement = (WebElement)js.executeScript("return document.elementFromPoint(arguments[0], arguments[1])", coordinates.getX(), coordinates1.getY()-1);
System.out.println(theElement.getAttribute("tagName"));
System.out.println(theElement.getAttribute("class"));
old:
you are using negative value in getY()-1 which mean coordinates above the element, it maybe the parent or preceding-sibling of element try to select it using xpath
WebElement popcodeBefore = objDriver.findElement(By.xpath("//*[#id='id_popcode']/parent::*"));
// Or
// WebElement popcodeBefore = objDriver.findElement(By.xpath("//*[#id='id_popcode']/preceding-sibling::*"));
System.out.println(popcodeBefore.getAttribute("class"));
actions.moveToElement(popcodeBefore).doubleClick().build().perform();
If you have any specific text at that particular coordinates make use of it. I too had the same issue like this where I need to double click on a cell which had text 0.00%. I have done hovering action first using the text and then performed the double-click
Ignore the syntax issues since I am working on the protractor these days
browser.driver.actions().mouseMove(driver.findElement(by.xpath("//*[text()='00')]").build().perform();
and then perform the click
Still, you have issues, check if you have any attribute like ng-click which can be helpful to get the coordinates for that particular location. please always share the HTML code so that It may help us to check more deeply

How to click an element with reference to another web element in Selenium WebDriver(Java)?

There are many span tags as mentioned in the image below and each has its own a-tag with unique id as "chooseitem". I need to choose particular a tag using names in the span tags.
Need to click the a-tag button using the text Mayo Chicken from the above HTML snippet in the image.
I have tried the below Selenium script
WebElement select = driver.findElement(By.xpath("//*[contains(text(),'Mayo Chicken (Single)')]"));
WebElement add = select.findElement(By.id("chooseitem"));
It doesn't work for me.
driver.findElement(By.id("chooseitem"));
The above code chooses the first item in the page by default as its id is also 'chooseitem', but need to define what to be chosen.
Can anybody help me out?
We need to get the common parent(ancestor) element of the chicked and the clickable 'a' tag, then we can navigate to the tag 'a'. Below xpath should ideally work.
"//span[contains(text(),'Mayo chicken')]/ancestor::div[4]//a"
Note: Here i have used div[4] because fourth parent is the common ancestor for 'Mayo chicken' and tag 'a'.
For more details about different xpath axis refer this->https://www.w3schools.com/xml/xpath_axes.asp
Hope this helps you. thanks.
You can do that by using the xpath position, press F12 for developer tools click on "Select element button", click the element that interests you on the page, as in your picture you will see one or more lines highlighted, right click the line -> Copy -> Copy xpath. You will have something like the line below:
//*[#id="comment-76500216"]/td[2]/div/span[1]
The xpath position will be:
//td[2]/div/span[1]
You can use that when you have multiple elements that share the name or id or so on.
And you will have:
WebElement select = driver.findElement(By.xpath("//td[2]/div/span[1]"));
PS: I used google chrome

selenium (phpunit) click on drop down link

I want be able click on link from drop down using selenium with phpunit. I don't have any idea how make it happens, can anyone show me example or relevant tutorial, post or anything that can help me figure out.
when I try click on the element without put mouse over the drop down I got this error:
Element is not currently visible and so may not be interact with command ....
Thanks.
EDIT:
when I said "drop down" I don't mean regular select. it more like popup
you can see the example here:
http://investing.com
look how they build the menu I want be able click on 'Technical' -> 'Fibonacci Calculator' for example.
Check this post out:
Selenium: How to select an option from a select menu?
You can find more info about this here
select(selectLocator, optionLocator)
Arguments:
selectLocator - an element locator identifying a drop-down menu
optionLocator - an option locator (a label by default)
Select an option from a drop-down using an option locator.
Option locators provide different ways of specifying options of an HTML Select element (e.g. for selecting a specific option, or for asserting that the selected option satisfies a specification). There are several forms of Select Option Locator.
label=labelPattern: matches options based on their labels, i.e. the visible text. (This is the default.)
label=regexp:^[Oo]ther
value=valuePattern: matches options based on their values.
value=other
id=id: matches options based on their ids.
id=option1
index=index: matches an option based on its index (offset from zero).
index=2
If no option locator prefix is provided, the default behaviour is to match on label.
Credits go to Dave Hunt
What I use:
$search13 = $this->webDriver->findElement(WebDriverBy::id('id_of_dropdown_field'));
$search13->click(); // Clicking on the dropdownfield
$this->webDriver->getKeyboard()->pressKey('ARROW_DOWN'); // Will go down in your dropdown selection )
sleep(1);
$this->webDriver->getKeyboard()->pressKey('ENTER'); // Enter for submitting your selection
EDIT:
http://www.anitpatel.net/2012/02/25/selenium-webdriver-how-to-click-on-a-hidden-link-or-menu/
This one explains it in java but basically what he does is a mouse over/hover and wait.
Then he clicks on the element.
I'm not a java genius but it's an example how to work with it.
You can use the:
string mouseOver(string $locator)
This simulates a user hovering a mouse over the specified element.
http://docs.tadiavo.com/phpunit/www.phpunit.de/pocket_guide/3.1/en/selenium.html
Check if the element is visible using the xpath of the required option value.
$this->isElementPresent($xpath);
$this->click($xpath);
If it is true, then click() method selects the specified option.