Interact with pseudo-elements with Selenium Webdriver? - selenium

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();

Related

Robot Framework: get element by text

I'm testing a website which is not exactly designed for automation testing. I'm using Robot Framework, Selenium2Library. There's an element that I cannot locate it by x-path the web driver will always locate to another element. Is there anyway to get the element by its text ?
The example HTML looks like
<label>Next Steps</label>
Is there any additional library that allows something like
Get Element By Text Next Steps
Please suggest.
You can get that element by text using xPath as well:
//label[text()="Next Steps"]
You could use
Wait Until Element Is Visible //label[.='Next Steps']
Recommended pages:
https://robotframework.org/SeleniumLibrary/SeleniumLibrary.html
https://robotframework.org/SeleniumLibrary/Selenium2Library.html
https://www.w3schools.com/xml/xpath_syntax.asp
Wait until element is visible, set focus and then get element by text :
Wait Until Element Is Visible //label[text()="Next Steps"] timeout=${time}s
Set Focus To Element //label[text()="Next Steps"]
Get Text //label[text()="Next Steps"]

Couldn't locate the search bar

I need to locate a search bar to search some text listed in the below box and click on it. I tried below code but I couldn't perform the activity.
This code wasn't clicked the search bar:-
driver.findElement(By.xpath("//input[#class='Searchbar__search-field___2FQ0S search-input']")
Image of the HTML:
The desired elements are ReactJS enabled elements within a Modal Dialog so to locate the element you have to induce WebDriverWait for the element to be clickable and you can use either of the following Locator Strategies:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.modal-body#promotion-url-modal-body input.search-input[placeholder='Find a promotion...']"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#class='modal-body' and #id='promotion-url-modal-body']//input[contains(#class, 'search-input') and #placeholder='Find a promotion...']"))).click();
Do you need to switch to a new frame before doing anything in the modal dialog? Do this:
driver.switchTo().activeElement();
And then try the following CSS locator:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("#promotional-url-modal-overlay input[placeholder='Find a promotion...']"))).click();
Can you provide us with a link to the website you are trying to interact with? Chances are the element is within an iframe, in which case you need to switch to the iframe before you can interact with said element.
Make sure your window is maximised, as this can interfere with iframes:
driver.manage().window().maximize();
Then you will need to find the id of the iframe, using something like firebug
Once you have the id:
driver.switchTo().frame("xxxxxxxxx");
interact with said element within the iframe:
driver.findElement(By.xpath("html/body/a/img")).click();
class can be changed, you need to check HTML on failed step. As a workaround you can use more generic xpath:
driver.findElement(By.xpath("//input[contains(#class, 'search-input')]"))

WebElement is identified through xpath in the developer console but the element wont be identified through selenium findElement method

The html element Im trying to locate is the "Shared" link.
I wrote a dynamic xpath to locate the element and it shows as the element was identified in the developer console.
But when i use the xpath that i wrote in the developer console to locate the element using selenium, it does not locate the element.
The method i used to check if it locates the element is shown below.
I could not figure out why this issue occurs, Is it because of a issue in the xpath that i have written or because of another issue?
Code you can try out is :
new WebDriverWait(driver,10).until(ExpectedConditions.elementToBeClickable(By.xpath(" your Xpath ")));
driver.findElement(By.xpath("your Xpath")).click();
The Xpath you have written would work if only one title is present on current page.
driver.find_elements doesn't have attribute to click() so use driver.findElement instead of Elements

How to ignore `other element will receive the click` and still click on the element

I am using Java and Selenium to write a test. I am going to click on a web element that is covered by another one so I receive the error other element will receive the click. I do NOT want to use Select so how can I click on the web element that is covered?
These are things that I have tried:
action.click(dropdown).build().perform();
action.moveToElement(dropdown).sendKeys(Keys.ENTER).build().perform();
dropdown.click();
Also there is no need to use scroll as the element is on the page but just covered by something else.
You can use JavascriptExecutor
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].click();", element);
I would not do this as it could miss potential defects. Not sure why you want to perform a click on hidden element.

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

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']")));