ankitashri8009
ankitashri8009

Reputation: 21

how to find xpath in selenium webdriver when button webelement is dynamic

Following is the html for the page and I need to get the xpath for the button which will work in selenium webdriver. or is there any way to click on the button in selenium webdriver If its in java that would be great otherwise python and others languages are also appreciated.

<button style="" onclick="saveAndContinueEdit('http://192.168.1.99/cue_extensions/magento1.9/index.php/admin/catalog_product/save/back/edit/tab/{{tab_id}}/id/899/key/6ed7b01b3a2136d6ff896d9380e281d5/')" class="scalable save" type="button" title="Save and Continue Edit" id="**id_636f54090db18b58eecba78e9e6aa6b7**">
  <span><span><span>Save and Continue Edit</span></span></span>
</button>

but when next time i logged in the html changes to

<button style="" onclick="saveAndContinueEdit('http://192.168.1.99/cue_extensions/magento1.9/index.php/admin/catalog_product/save/back/edit/tab/{{tab_id}}/id/903/key/29d8597a8ec8f63d9b09addae444d4c0/')" class="scalable save" type="button" title="Save and Continue Edit" id="id_c2fe56a74ef2a166dcf0ae4ed7f8e391">
  <span><span><span>Save and Continue Edit</span></span></span>
</button>[![enter image description here][1]][1]

Upvotes: 2

Views: 53744

Answers (5)

ninad
ninad

Reputation: 9

In java you find using following command:

driver.findElement(By.Xpath("//button[contains(.,'Save and Continue Edit')]")).click();

Upvotes: 0

Vikas Ojha
Vikas Ojha

Reputation: 6950

You can use the following xpath -

//span[@title="Save and Continue Edit"]

Upvotes: 0

Fabian Schmengler
Fabian Schmengler

Reputation: 24576

If you can assume that the title is always "Save and Continue Edit" (i.e. the Magento backend language is not changed), use

//button[@title='Save and Continue Edit']

as suggested before. A more robust approach is to search by the onclick attribute, for example with:

//button[starts-with(@onclick,"saveAndContinueEdit(")]

Upvotes: 1

drkthng
drkthng

Reputation: 6929

in Java:

driver.findElement(By.xpath("//button[@title='Save and Continue Edit']")).click();

in python

driver.find_elements_by_xpath("//button[@title='Save and Continue Edit']").click()

Upvotes: 1

ettanany
ettanany

Reputation: 19816

Try the following:

In Java:

driver.findElement(By.Xpath("//span[contains(text(), 'Save and Continue Edit')]")).click();

In Python:

driver.find_element_by_xpath("//span[contains(text(), 'Save and Continue Edit')]").click()

Upvotes: 1

Related Questions