I am trying to click the button first which I did and then the dropdown menu element which is Tüm Soru Tipleri automatically in Selenium Java.
This one is didn't work:
driver.findElement(By.id("select2-question_types-sq-result-xih0--1")).click();
Could you help?
HTML snapshot:
Element snapshot:
Try adding a wait of 2 ms between button and drop-down.
Once you click and expand the dropdown further to click() on the desired <li> element with text as Tüm Soru Tipleri you need to induce WebDriverWait for the elementToBeClickable() and you can use the following locator strategy:
Using xpath and text():
new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[#class='select2-results']/ul[#id='select2-question_types-sq-results']//li[text()='Tüm Soru Tipleri']"))).click();
Using xpath and contains():
new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[#class='select2-results']/ul[#id='select2-question_types-sq-results']//li[contains(., 'Tüm Soru Tipleri')]"))).click();
Related
I have a problem with clicking on svg element in selenium Java.
HTML code:
I have tried next code examples but they are not helped.
Actions action = new Actions(webDriver);
action.click(webElement).build().perform();
JavascriptExecutor executor = (JavascriptExecutor) webDriver;
executor.executeScript("arguments[0].click;", webElement);
Actions action = new Actions(webDriver);
Thread.sleep(2000);
action.moveToElement(webElement).click().build().perform();
I did not get any exception or error. But can not click on SVG element
Which solutions are exist for it?
The desired element is a svg element so to click() on the element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following locator strategies:
selenium4 compatible code
Using cssSelector:
new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div[placement='bottom'] svg"))).click();
Using xpath:
new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#placement='bottom']//*[name()='svg']"))).click();
References
You can find a couple of relevant detailed discussions in:
How to click on SVG elements using XPath and Selenium WebDriver through Java
I'm trying to do a simple selenium java automation for automating "sign in and sign out of amazon.com site". I'm able to sign in using element locator techniques, like XPath and CSS selector. But for signout, I'm thrown with ElementNotInteractable exception.
Below is the code that I tried(posting the code segment of signout alone).
WebElement element1 = driver.findElement(By.xpath("//header/div[#id='navbar']/div[#id='nav-belt']/div[3]/div[1]/a[1]/span[1]"));
element1.click();
driver.findElement(By.xpath("//a[#id='nav-item-signout']")).click();
I have tried the above code segment with different element locator techniques like CSS selector and etc, but no luck.
Kindly suggest if I can find and click the sign-out link in the flyout menu by any other method.
Thanks.
You can try below code in which explicit wait is implemented so it will wait for the element to click
WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement element1 =driver.findElement(By.xpath("//header/div[#id='navbar']/div[#id='nav-
belt']/div[3]/div[1]/a[1]/span[1]"));
element1.click();
ele2=driver.findElement(By.xpath("//a[#id='nav-item-signout']"))
wait.until(ExpectedConditions.elementToBeClickable(ele2));
ele2.click();
Instead of the click() method try this :
WebElement element1 = driver.findElement(By.xpath("//header/div[#id='navbar']/div[#id='nav-belt']/div[3]/div[1]/a[1]/span[1]"));
element1.sendKeys(Keys.RETURN);
driver.findElement(By.xpath("//a[#id='nav-item-signout']")).sendKeys(Keys.RETURN);
And instead of RETURN you can also try ENTER
You can try below code in which mover hover is implemented so it will hover the menu then you can click for sign-out.
WebElement ele = driver.findElement(By.id("nav-link-accountList-nav-line-1"));
Actions action = new Actions(driver);
action.moveToElement(ele).perform();
driver.findElement(By.xpath("//*[#id='nav`enter code here`-item-signout']/span")).click();
I want to click a button(to send a form)
<button class="form-button primary">Click here</button>
I find the element like this:
driver.findElement(By.xpath("//button[contains(text(),'Click here')]")).click;
On chorme is working and sending the form but in IE11 is not(sending the form).
To be clear, in IE is finding the element(or an element). But probably is not the correct element.
Addition info:
This is the only button with this text
I can probably find other ways to get this element , but if I rework this path I will need to change all the paths similar to this.
Selenium version:3.14
IE webdriver :3.14
There are several different ways of clicking on something using selenium. I would try using either a javascript click or an action click.
Javascript click:
WebElement element = driver.findElement(By.xpath("//button[contains(text(),'Click here')]"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
Action click:
Actions action = new Actions(driver);
WebElement element = driver.findElement(By.xpath("//button[contains(text(),'Click here')]"));
action.moveToElement(element).click().build().perform();
It's also possible that you are in the wrong frame while you are executing the click.
driver.switchTo.frame("Frame_ID");
You would be able to find the frame ID when you inspect the webpage.
If your usecase is to invoke click() you have to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:
xpath using className and text:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[#class='form-button primary' and text()='Click here']"))).click();
xpath using text:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[text()='Click here']"))).click();
xpath using contains():
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(., 'Click here')]"))).click();
I am trying to click on a hyperlink with a space using Java and Selenium. Here is a sample of the code
<h3 class="side menu">
<a class="side-menu" href="/configurations">
<span class="menu-icon ca"></span>Configuration
</a>
</h3>
I have tried using xpath starts-with and contains with no luck.
driver.findElement(By.xpath("//a[starts-with(text(),’Configuration’)]")).click();
To handle dynamic element use WebDriverWait and elementToBeClickable with expected conditions.Use the following Xpath locator strategies.
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath('//a[#class="side-menu"][normalize-space(.)="Configuration"]')));
element.click()
OR
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath('//a[#class="side-menu"][contains(.,"Configuration")]')));
element.click()
I think all you need is to add a forward slash:
driver.findElement(By.xpath("//a[starts-with(text(),'/configuration')]")).click();
It works for me in the Chrome console if I type:
$x("//a[starts-with(#href,'/configuration')]")
I would use normalize-space when writing the xpath for the text, which will trim the leading and preceding white chars (spaces/tabs/newlines)
Here is how you should do it in your question.
//a[normalize-space(.)='Configuration']
Tested the xpath in chrome console for click() successfully using javascript.
I am attempting to click on 'Log in' but my Selenium code isn't working. Below is the HTML code.
<span class="css-14krylx-text-text-fullPageText-FormFooter">Already have a Times account?
<span tabindex="0" data-testid="switch-to-login" class="css-dip6gw-link-link-FormFooter">Log in</span></span>
What I attempted so far and did not work?
driver.findElement(By.cssSelector("[data-testid='switch-to-login'")).click();
driver.findElement(By.xpath("//span[#data-testid='switch-to-login'")).click();
driver.findElement(By.cssSelector("/.css-dip6gw-link-link-FormFooter'")).click();
Is there any other approach?
The desired element is a dynamic element 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("span[class$='text-text-fullPageText-FormFooter'] span[class$='link-link-FormFooter'][data-testid='switch-to-login']"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[contains(#class, 'text-text-fullPageText-FormFooter')]//span[contains(#class, 'link-link-FormFooter') and text()='Log in']"))).click();