How to use regex in Selenium Webdriver xpath? - java

I have the following WebElement which can locate using xpath:
/html/body/div/div/div/div[2]/div[1]/div[4]/div/div/div[335]/div[1]/span
In the above locator, div[335] number randomly changes. I want to use regex and use the xpath as element locator.
Can anyone please let me know the regular expression to use with the above xpath?
I'm using Selenium Webdriver with Java. IDE is Intellij and I'm testing on Chrome browser.
I'm going to use the above mentioned xpath as below:
#FindBy(how = How.XPATH, using =/html/body/div/div/div/div[2]/div[1]/div[4]/div/div/div[335]/div[1]/span
public WebElement totalSuppliers;

Related

How to use selenium on websites which use ajax?

I have tried using selenium to automate a website and select a option from the drop-down menu, but the problem I am facing is that after the option is selected the ajax is not executed. This only happens when using selenium, I had tried this manually and it worked. Webdriver wait is not working for me. Here is the code that does that:
WebElement element =(driver.findElement(By.id("equity_optionchain_select")));
Select elementSelect=new Select(element);
elementSelect.selectByVisibleText("BANKNIFTY");
As far as BANKNIFTY is concerned, presumably those elements will be dynamic elements i.e. either JavaScript, ReactJS, jQuery, Ajax, Vue.js, Ember.js, GWT, etc. based elements. So ideally you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:
Using id and selectByVisibleText():
new Select(new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.id("equity_optionchain_select")))).selectByVisibleText("BANKNIFTY");
Using cssSelector and selectByVisibleText():
new Select(new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("#equity_optionchain_select")))).selectByVisibleText("BANKNIFTY");
Using xpath and selectByVisibleText():
new Select(new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//*[#id='equity_optionchain_select']")))).selectByValue("BANKNIFTY");

How to locate an element with reference to an element in Page Object Model

I want achieve similar functionality of webElement.findBy(By.xpath("xpath_locator")); in Page Object Model framework with #FindBy annotation.
For example:
#FindBy(how = How.XPATH, using = "xpath_locator_1")
public WebElement wbel_1;
I want to find out next WebElement with reference to "wbel_1", how can I achieve this?
Xpath Axes are the solution.You can use the preceding and following functions of xpath for this.
xpath_locator_1/following::tagname[n]
following selects everything in the document after the closing tag of the current node.
Please check out this link:
https://www.w3schools.com/xml/xpath_axes.asp

Unable to automate HTML5 DragAndDrop in IE11

Are there any work around to make HTML5 Drag and Drop work in IE11 with Selenium Webdriver with java? I am using Selenium-Webdriver 3.4
Please find the attached for more details about the HTML. I have used the below Locator to locate the elements which works fine with Action class in chrome but not in IE11.
Web Element Initialisation
#FindBy(how = How.CSS, css =".work-card-description.searchable-field")
private WebElement weDragFrom;
#FindBy(how = How.CSS,css = "div.resource-schedule.resource-ui-droppable
div.hour-line:nth-of-type(11)")
private WebElement weDragTo;
Please find the Attached

How to remove a web-element using in selenium using Java?

Is there any way by which we can remove a web-element from the the source of the page.
I have the problem that there is a division of popup in the webpage source which is disabling all other divisions. If we remove this division while doing inspect element from a browser all other elements are automatically enabled for use.
How can I do this in selenium webdriver?
To my knowledge, there is no direct Java method, but you can use the JavaScriptExecutor API to do something like:
WebElement yourElement = …
JavaScriptExecutor jsExecutor = (JavaScriptExecutor) webDriver;
jsExecutor.executeScript(
"arguments[0].parentNode.removeChild(arguments[0])", yourElement);

Selenium doesn't recognise xpath

I am using Selenium WebDriver to get from a drop down list values. Unfortunately I can't get it, because my code can't recognise the xpath.
Here's my code:
WebElement selector = driver.findElement(By.xpath("id('search')/x:fieldset/x:table[1]/x:tbody/x:tr[2]/x:td[1]/x:select"));
Select s = new Select(selector);
List<WebElement> options = s.getOptions();
for (WebElement wb : options) {
System.out.println(wb.getText());
}
The problem is with the 1st line (WebElement selector). In output I get something like this:
Exception in thread "main"
org.openqa.selenium.InvalidSelectorException: The xpath expression
'id('search')/x:fieldset/x:table[1]/x:tbody/x:tr[2]/x:td[1]/x:select'
cannot be evaluated
I've even tried to find by name or class, but selenium still doesn't find this list.
How to solve the problem? Thanks in advance :)
Ganjira, This is no way to write a xpath. If you find it tough to recognize item . Use Selenium IDE 'Select' button.
If you can provide sample of html page on which you try to find your element, it would be very helpful. Anyway, try to search using css selectors e.g.
WebElement selector = driver.findElement(By.css("#search > select"));

Categories