How can I click this using selenium webdriver? - selenium-webdriver

I have an element like this:
<a class="btn">Select</a>
How do I click this using selenium webdriver?

To click() on the link with text as Select you can use either of the following options :
Python link_text :
driver.find_element_by_link_text("Select").click()
Python xpath :
driver.find_element_by_xpath("//a[#class='btn' and contains(.,'Select')]").click()
Java linkText :
driver.findElement(By.linkText("Select")).click();
Java xpath :
driver.findElement(By.xpath("//a[#class='btn' and contains(.,'Select')]")).click();

Related

"How to solve Selenium Webdriver path error ?"

I stored it in d Drive and gave loction of webdriver but it gives an error
from selenium import webdriver
chrome_driver_path = "D:\chromedriver.exe"
url = "https://www.linkedin.com/jobs/search/?f_LF=f_AL&geoId=102257491&keywords=python%20developer&location=London%2C%20England%2C%20United%20Kingdom&redirect=false&position=1&pageNum=0"
driver = webdriver.Chrome(executable_path=chrome_driver_path)
driver.get("url")
you should try this: driver.get(url)
not this: driver.get("url")

webdriverIO 5, how to switch to iframe when its element 'id' not given?

frame html code:
<iframe title="frame report" class="ReportViewer__iframe" src="/yesyyy.aspx?reportId=145&ts=1550681978158&bgcolor=#f8f9fb" width="100%" height="100%"></iframe>
script fails both for title and class name locators -- 'ERROR webdriver: Request failed due to Error: no such frame'
Use the following code.
driver.switchTo().defaultContent();
WebElement frameXpath = driver.findElement(By.xpath("//iframe[#title='frame report']"));
driver.switchTo().frame(frameXpath);
Got the solution, pass the object of the iframe with available locators (title or css class)
browser.switchtoframe($('.report__iframe'))
HTML Code:
iframe id="ifr" name="demo" src="demo.html" height="200" width="300">
Switch statement:
browser.switchToFrame($("//iframe[#src='demo.html']"))
For more info read here: https://chercher.tech/webdriverio/iframes

Selenium Webdriver angular.js button

I have the code which I am trying to run in Selenium webdriver. It is a button but I can't select it with java code. Can you please help me which is true ?
<mat-list-item _ngcontent-c7="" class="menu-item mat-list-item ng-star-inserted" id="kisiler"><div class="mat-list-item-content"><div class="mat-list-item-ripple mat-ripple" mat-ripple=""></div><div class="mat-list-text"></div>
<!----><mat-icon _ngcontent-c7="" class="mat-icon material-icons ng-star-inserted" role="img" aria-hidden="true">group</mat-icon>
<!---->
<!----><mat-label _ngcontent-c7="" class="ng-star-inserted">Kişiler</mat-label>
</div></mat-list-item>
My code is:
driver.findElement(By.id("[#id='kisiler']")).click();
To click on the element with text as Kisiler you need to induce WebDriverWait for the element to be clickable and you can use the following solution:
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//mat-list-item[#class='menu-item mat-list-item ng-star-inserted' and #id='kisiler']//mat-label[#class='ng-star-inserted']"))).click();

Hiding the CMD window of PhantomJS on python with selenium

I am trying to hide the CMD window that appears when launching PhantomJS with python:
from selenium import webdriver
browser = webdriver.PhantomJS()
There appears to be a solution when working with C#, however, I was not able to find anything similar for python. Since the feature for C# was added back in 2014, I assume something similar should exist for python too.
I'm using latest PhantomJS and Python 3.6.2.
This is how it looks like:
For me,
The below code works fine.
driver = webdriver.PhantomJS(
service_args=service_args,
desired_capabilities=caps,
executable_path='phantomjs.exe',
service_log_path=None
)
Here you have to add service_log_path=None for no log file that's why the console window will get hidden.
Still Not solved then
replace code from
(C:\Users\YOUR_USER_NAME\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\selenium\webdriver\common\service.py) file
self.process = subprocess.Popen(cmd, env=self.env, close_fds=platform.system() != 'Windows', stdout=self.log_file, stderr=self.log_file, stdin=PIPE)
to
self.process = subprocess.Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=False, creationflags=0x08000000)

Unable to locate link using selenium web driver

Could you please help me to locate a link Taschen in the below piece of code?
Code:
<a data-flyout-target="handbags" title="Taschen" href="#">Taschen</a>
There are multiple ways by which you can identify this link:-
By.Xpath
By.LinkText
Xpath:-
//a[text()='Taschen']
//a[#data-flyout-target = 'handbags']
//a[#title='Taschen']
So you can identify it using driver object like:-
driver.findElement(By.xpath("Put any of the above xpaths"));
Or Using LinkText
driver.findElement(By.linkText("Taschen"));
Hope it helps!

Resources