selenium webdriver find_element_by_xpath - selenium

I am trying to click on the accept button using xpath in www.simulator.betfair.com but it keeps giving me the error down below
selenium.common.exceptions.NoSuchElementException: Message: no such
element: Unable to locate element:
{"method":"xpath","selector":"//*[#id="onetrust-accept-btn-handler"]"}
(Session info: chrome=90.0.4430.85)
Any ideas? this is my code
from selenium import webdriver
url = r"C:\Users\salde\Desktop\chromedriver_win32 (1)\chromedriver.exe"
driver = webdriver.Chrome(executable_path=url)
driver.get('https://simulator.betfair.com/')
cookies = driver.find_element_by_xpath('//*[#id="onetrust-accept-btn-handler"]')
cookies.click()

Related

clicking button with selenium webdriver

I am trying to click a button with selenium webdriver. But not managing to identify the button.
This is my code: It returns an error message
NoSuchElementException: Message: u'no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id="artifactContentList"]/div[2]/div/div[3]/content-dataset-actions/content-dataset-actions/section/section/button[2]"}\n (Session info: headless chrome=81.0.4044.138)\n (Driver info: chromedriver=2.39.562718 (9a2698cba08cf5a471a29d30c8b3e12becabb0e9),platform=Windows NT 10.0.18363 x86_64)'
from selenium.webdriver.chrome.options import Options
from splinter import Browser
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import csv
chrome_options = Options()
chrome_options.add_argument("--headless")
browser = webdriver.Chrome(executable_path= "C:\chromedriver\chromedriver.exe", chrome_options=chrome_options)
def open_url():
browser = webdriver.Chrome(executable_path= "C:\chromedriver\chromedriver.exe", chrome_options=chrome_options)
browser.get("https://app.powerbi.com/groups/xxxxs")
python_button = browser.find_element_by_xpath('/html/body/div[1]/root-downgrade/mat-sidenav-container/mat-sidenav-content/div/landing/div/div/div/ng-transclude/landing-route/list-proxy/content-list/div/main/dataset-list/div/virtual-scroll/div[2]/div/div[3]/content-dataset-actions/content-dataset-actions/section/section/button[2]')
print(python_button)
python_button.click()
browser.quit()
open_url()
This is the full element:
<button class="refreshNow pbi-glyph pbi-glyph-refresh" localize-tooltip="RefreshNow" ng-if="::$ctrl.canRefreshNow" ng-click="$ctrl.runAction($ctrl.RefreshNow)" aria-describedby="ModeldatasetMenu4" use-tooltip-as-aria-label="" title="Refresh now" aria-label="Refresh now" pbi-focus-tracker-idx="54"></button>
Try this:
python_button = browser.find_element_by_class_name('refreshNow')

Clicking on tab locator for selenium webdriver java

I am having trouble finding out the correct locator for clicking on the tab after logging into the a page
HTML:
<td title="Maintenance" id="c1_tab3" class="tabTab noselect tabSelectedTab" onclick="getcontrol( 'c1').setvalue(3);">Maintenance</td>
Test:
#Test
public void Case1() {
driver.navigate().to(URL);
//driver.findElement(By.linkText("Transcode Service")).click();
driver.findElement(By.id("c1_tab3")).click();
Error:
FAILED: Case1
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"c1_tab3"}
(Session info: chrome=70.0.3538.102)
can you try this xpath:
driver.findElement(By.xpath("//td[#id='c1_tab3' and contains(.,'Maintenance')]")).click();

Selenium: Unable to locate email type box, not within any iframe

I am trying to detect the login id and password field of a website : https://mretailstore.com/login but seems selenium is not able to locate the email type box. I have checked stackoverflow but didn't get any solution to this. Someone has used iframe because of what he/she was facing the same issue but here we have not incorporated any iframe.
The error I am getting is:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: .//*[#id='identity']
The code I am using:
System.setProperty("webdriver.gecko.driver", "C:\\Users\\MI SERVICE\\Downloads\\geckodriver.exe");
FirefoxOptions capa = new FirefoxOptions();
capa.setCapability("marionette", true);
WebDriver driver = new FirefoxDriver(capa);
driver.get("https://www.mretailstore.com/");
driver.findElement(By.xpath(".//*[#id='identity']")).sendKeys("abc#d.com");
driver.findElement(By.xpath(".//*[#id='password']")).sendKeys("abc123");
driver.findElement(By.id("loginbutton")).click();
driver.navigate().back();
driver.close();
It looks your xpath is correct only and this exception is happening before element rendering.So, Please add the some explicit wait after the page loading.
It is working for me with/without Explicit Wait.
Code:
driver.get("https://www.mretailstore.com/");
WebDriverWait wait=new WebDriverWait(driver,20);
wait.until(ExpectedConditions.titleIs("Login"));
driver.findElement(By.xpath(".//*[#id='identity']")).sendKeys("abc#d.com");
driver.findElement(By.xpath(".//*[#id='password']")).sendKeys("abc123");
driver.findElement(By.id("loginbutton")).click();

Selenium - How to Click a Link by href value in WebDriver

I have this piece of code
<a href="/iot/apply/device.do" id="subMenu1" class="fortification55"
onclick="$('#deviceinfo').hide()">Apply</a>
I am trying to link by href using
getDriver().findElement(By.name("subMenu1")).click();
But i got this error
org.openqa.selenium.NoSuchElementException: Unable to find element with name == subMenu1 (WARNING: The server did not provide any stacktrace information)
As the element is having the onclick Event, the WebElement is a JavaScript enabled element. So to invoke click() on the element you need to use WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:
Using cssSelector and only href attribute:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a[href='/iot/apply/device.do']"))).click();
Using xpath and only href attribute:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[#href='/iot/apply/device.do' and text()='Apply']"))).click();
Using a canonical cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.fortification55#subMenu1[href='/iot/apply/device.do']"))).click();
Using a canonical xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[#class='fortification55' and #id='subMenu1'][#href='/iot/apply/device.do' and text()='Apply']"))).click();
Note : You have to add the following imports :
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.By;
References
You can find a couple of relevant discussions on NoSuchElementException in:
NoSuchElementException, Selenium unable to locate element
Exception in thread “main” org.openqa.selenium.NoSuchElementException: Unable to locate element: //*[#id='login-email']
the following code should work :
By.xpath("//a[#href='/iot/apply/device.do']")

How to use gethtmlsource or storeHtmlSource in Selenium Webdriver(Java)?

i want to print html page source in output. In selenium IDE there is a command to perform this action.
storeHtmlSource
How to do the same in Selenium Webdriver(Java)?
While exporting the testcase, It shows the following error.
// ERROR: Caught exception [ERROR: Unsupported command [getHtmlSource | | ]]
Using Selenium WebDriver:
WebDriver driver = new ChromeDriver();
driver.get("your Url here");
String htmlSource = driver.getPageSource();
Api doc to get the page source
https://seleniumhq.github.io/selenium/docs/api/java/