Web Scraping: How to click a specific href link in selenium python? - python

I wanted to automate a web page using webdriver(Python) wherein the script will click on the href link directly in order to navigate to other page. Couple of details in the specific tag:
<span class = "xyz", ui-sref ="abc", href= "/letters/letters_number/">
"hdf"
<i class = 'icons'>letters</i>
</span>

To click() on a the element with the href attribute you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span[href=/letters/letters_number/] i.icons"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[#href='/letters/letters_number/']//i[#class='icons']"))).click()
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Related

Python Selenium: Click element by custom attributes

I am trying to click this button with Selenium:
<button class="MuiButtonBase-root MuiButton-root jss38 MuiButton-contained MuiButton-containedPrimary" tabindex="0" type="button" data-test="unifiedCrazyButton"><span class="MuiButton-label">Let's get crazy</span><span class="MuiTouchRipple-root"></span></button>
I can't do it by the class name, because there are other buttons with the same class name, and also the xpath value in this format:
/html/body/div[10]/div[3]/div/div[3]/button[2]
keeps changing which is unfortunate.
The only identifying factor seems to be
data-test="unifiedCrazyButton"
How can I click this button with Selenium?
The desired element is a dynamic element, so to click on the clickable element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[data-test='unifiedCrazyButton'] span.MuiButton-label"))).click()
Using XPATH:
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[#data-test='unifiedCrazyButton']//span[#class='MuiButton-label' and contains(., 'get crazy')]"))).click()
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

How to click on a link with Selenium with href="javascript:__doPostBack

I tried to click link from selenium.webdriver but I got it nothing. Can please help with this issue
Page contains the following elements:
Details
My target to click:
"m_m_cBody_bdy_uc_tbl$Edit","13911"
Please note that I have many text links with
Details
is not a unique element on my page
<a href="javascript:__doPostBack("m_m_cBody_bdy_uc_tbl$Edit","41946")">
Details </a>
To click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using XPATH and normalize-space():
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[normalize-space()='Details' and contains(#href, '13911')]"))).click()
Using XPATH and contains():
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(., 'Details') and contains(#href, '13911')]"))).click()
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Handle a click when hyperlinks are not visible

I am new to selenium and trying to automate some web click using selenium and python
There is a link as per below, which opens up dialogue box
<li id='upFol'>
<a href='#' title='documents'></a>
.....
</li>
I have below code in python
upload = WebDriverWait(driver,5).until(EC.presence_of_element_located((By.XPATH,"//*[#id='upFol']/a")))
upload.click()
this finds the element and can see the click but dialogue box doesn't get opened
Is there way to handle these kind of scenarios
Instead of presence_of_element_located() you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "li#upFol > a[title='documents']"))).click()
Using XPATH:
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//li[#id='upFol']/a[#title='documents']"))).click()
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Python, Selenium: how to click on javascript:void(0) element

I have an "onclick" element in a webpage,
<a href="javascript:void(0)" onclick=
"toIndexHtml('3','http://xxxxxxxxxxx/trade','0')">
<i></i>
<span></span>
trade
</a>
It is shown in the webpage as a button and I want to click on it, I tried to search for it using the following code:
driver.find_element_by_xpath("//a[contains(#onclick,'toIndexHtml')]").click()
WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH,"/html/body/div/ul/li[3]/a"))).click()
Both are not working, please suggest if there is any other ways! Thanks in advance!
P.S.: I am using Chrome WebDriver and Chrome v64.
Your first locator looks perfect and should have worked.
Ideally, to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[onclick^='toIndexHtml']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[starts-with(#onclick,'toIndexHtml')]"))).click()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

How to click on a javascript/AJAX based element with onclick attribute using Selenium and Python

I'm trying to click a link in a dropdown menu in Selenium.
I'm accessing the element like so:
link = menu.find_element_by_xpath('//*[contains(text(), "Mark as shipped")]')
The link's href is javascript.void(0), and contains an onclick attribute which contains:
'com.ebay.app.myebay2.lineaction.service.LineActionAjax.processTransRequest("http://payments.ebay.com/ws/eBayISAPI.dll?OrderAction&transId=#TID#&action=4&pagetype=1883&ssPageName=STRK:MESO:SHP&itemid=_Item_Id", "_Item_Id", "987349587", "MarkShipped", "98739873", "_Item_Id_9874987_ss", 24")'
I've tried triggering this with:
click()
and
driver.execute_script(link.get_attribute('onclick'))
Also an ActionChain mousing over the link and clicking it.
But none seem to work. How do I trigger this?
The element is a AJAX element, so ideally to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using PARTIAL_LINK_TEXT:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "Mark as shipped"))).click()
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[onclick*='MarkShipped']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(#onclick, 'MarkShipped') and contains(., 'Mark as shipped')]"))).click()
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Categories