Can't click to Customize link when div+link together with FakeAnchor class using selenium web driver - selenium

Can't click to Customize link when div+link together with FakeAnchor class using selenium web driver
In my Ajax application, we have dropdown + link (Customize) together in div and i want to click to Customize link. I have locator and which was working fine for Customize link with old selenium but it doesn't with latest web driver. Can anyone please point me the problem or suggest something to make it work?
Expected:
Clicking to Customize link should open respected option (it actually opens dialog).
Actual:
Below locator clicks to dropdown button instead of Customize link due to such a complex page DOM which has no actual href or anchor tag.
Locator:
css=div[id$='_repeatDesc'][class='FakeAnchor']
Html:
<div id="zcs1_repeatDesc" class="FakeAnchor" style="cursor: pointer;">Customize</div>
Code:
webDriver().findElement(By.cssSelector("div[id$='_repeatDesc'][class='FakeAnchor']")).click();

I think your locator is not unique, may be it is locating dropdown element that's why it clicks to dropdown button instead of Customize link.
You should try using By.xpath() with text() node to locate this element as below :-
webDriver().findElement(By.xpath(".//div[text() = 'Customize']")).click();
Or As I'm seeing in HTML element has id attribute, if it's unique I'd to locate desire element and it's not being changed dynamically, you can try also using By.id() as below :-
webDriver().findElement(By.id("zcs1_repeatDesc")).click();
Edited :- If you want to click using JavascriptExecutor try as below :-
((JavascriptExecutor)driver).executeScript("arguments[0].click()", webDriver().findElement(By.xpath(".//div[text() = 'Customize']")));

Related

Interact with pseudo-elements with Selenium Webdriver?

I am working with Selenium Webdriver with Java.
And I was trying to interact with anchor tag which is enclosed as pseudo element ::before
But I am unable to interact with anchor element.
Here is the screenshot of the HTML structure.
With
JavaScriptExecutor, I understand, we can fetch the propertyValue using window.getComputedStyle().getPropertyValue() but I am not sure, how to interact with <a> element and execute a Click.
Initially, I attempted to click on the Anchor Element without considering the pseudo element as simple Element Interaction.
To fetch the Element:
private By tabRawView_By_CSS = By.cssSelector("[tabid='raw-view'][role='tab']");
But this piece is not throwing any error but it is also not clicking on the element.
Then I thought of using JavaScriptExecutor and was trying to run first in Developers Tool as below image but couldn't find suitable options.
Can anyone please suggest?
If it is Java bindings and all you want to do is to click on an achor tag which has Raw view as a text.
You could try with ExplicitWaits :
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.partialLinkText("Raw View"))).click();

How to find XPath of a class element that has more than one occurrences on the page?

I am trying to make Selenium click upvote buttons on a reddit-like website. The site has entries from different users and each entry has a upvote and downvote button below it. What i want to do is to make Selenium click on the first upvote button ( which belongs to the entry at the top ) on the page.
I tried to use "Copy XPath" function in Chrome, but all of the upvote buttons on the page return the same XPath:
//*[#id="eksico-chevron-up-thick"]/path
And this is how the website looks like if needed :
So, is there any way for finding the XPath of the first upvote button? I was thinking of something like:
//*[#id="eksico-chevron-up-thick"]/[1]
etc. Thanks in advance.
Edit: The HTML Code of one of the upvote elements:
The element that you are trying to click is under shadow dom as mentioned in the html structure and currently selenium does not support operation on the elements under the shadow dom.
Reference: https://medium.com/rate-engineering/a-guide-to-working-with-shadow-dom-using-selenium-b124992559f
So, if you want to click on the element, you can use JavaScriptExecutor like:
WebElement element = driver.findElement(By.id("eksico-chevron-up-thick"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
By default, it will click on the first element itself and if you want to click on a specific nth element then you can take the elements in a list and then send the index of that element inside the method to get that element clicked.
If you want to click on the first one you could use
Driver.find_elements_by_xpath(//*[#id="eksico-chevron-up-thick"]/path)[1].click()

How can i click on the login button in selenium webdriver of website Http://Phptravels.Com/Demo/

I want to click on login button. Which attribute should I use to click on login button? I am new to selenium web driver. I am unable to find its link text, id, class name, name. I am not able to find its XPath or CSS selector. please advice me with the code. Image is here
You can use either Xpath which is
.//*[#id='main-menu']/ul/li[8]/a
or CSS Selector which is
.login
To click on element with text as LOGIN within the url https://phptravels.com/404/ you can use either of the Locator Strategies:
CssSelector:
"a.login[href='http://phptravels.org']>span"
XPath:
"//a[#class='login' and #href='http://phptravels.org']/span[contains(.,'LOGIN')]"
//or
"//a[#class='login' and #href='http://phptravels.org']/span[contains(.,'Login')]"
You have multiple options, just avoid using xpath since its an overkill
li.user-login>a.login>span
or
a.login>span
or xpath
.//a[#class='login']/span[text()='Login']

How to select webElement present under form tag using selenium webdriver

How to click element present under Form tag in selenium web driver. using xpath, id, name it does not get recognized.refer screen shot
Here is a css selector:
.container-contact-info input[value='new']
Or an xpath
//*[#class='container-contact-info']//input[#value='new']
This might not work if you have a ton of these forms. If that's the case please update your question with the relevent information and I can adjust this answer.

Identifying the Web element which have same property (same class,same type,same name).Differance is only in value in selenium webdriver

After entering value”86100000” and clicking the “Search” button ,
Page will be reloaded and will get list of “Add to List” button under Commodity Section.
As “Search” and “Add to List” button have same property ,Not able to click “Add to List” button![Picture showing Search and Add to List Button]
HTML tag for "Search" Button :
HTML tag for "Add to List" Button :
Pls suggest way to identify "Add to List" button..I tried by xpath and CSS Selector.IDE is identifying that button.But in eclipse using selenium code is giving error and not able to locate element..
I need code to wait till the page gets loaded after clicking "Search" button and need to click ""Add to list" button..
Pls help me with ur ans..
Thanks in advance
search xpath = //input[#value='Search'
add to list xpath = //input[#value='Add to list']
In order to be able to help you, can you paste the HTML of the page before and after the search. Then I will be able to give you some sample code.
You can use the following XPath locator to locate the first button:
//input[#value="Search" and #type="button"]
In WebDriver C#
var searchButton = driver.FindElement(By.XPath("//input[#value='Search' and #type='button']"));
In order to locate the add to list button, use the following XPath:
//input[#value="Add to list" and #type="button"]
In WebDriver C#
var addToListButton = driver.FindElement(By.XPath("//input[#value='Add to list' and #type='button']"));
:nth-child(css selector) should be able to help you here.