How to click on the element using Selenium and Python - python

I want to click on "create post" button of this element:
<div class="l61y9joe j8otv06s a1itoznt qwtvmjv2 kiex77na lgsfgr3h mcogi7i5 ih1xi9zn ippphs35 a53abz89" data-hover="tooltip" data-tooltip-display="overflow" id="js_2f">Create Post</div>
But this id (id="js_2f") has a random value:
I did it this way but didn't work !
wait = WebDriverWait(self.browser, 20)
wait.until(EC.element_to_be_clickable((By.XPATH, "//*[text()='Create Post' and contains(#id, 'js_')]"))).click()
How can i click on it Using Xpath method ?

Following should help:
driver.find_element_by_xpath('//div[contains(text(),"Create Post")]').click()

You can try below xpath with contains
//div[contains(text(),'Create Post')]

You can try to perform a click on this element using link text
driver.find_element_by_link_text("Create Post")
or
driver.find_element_by_xpath("//div[text() = 'Create Post']")

The Create Post button is a JavaScript enabled element, so to click on the element you have 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, "input[data-hover='tooltip'][data-tooltip-display]"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#data-hover='tooltip' and text()='Create Post'][#data-tooltip-display]"))).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

How to click on button with text Show more results using Selenium and Python?

For this button:
I used these pieces of code, but it did not work.
WebDriverWait(wd, 1).until(EC.element_to_be_clickable((By.XPATH, "//input[contains(., 'Show more results')]"))).click()
and
button = wd.find_elements_by_xpath("//*[contains(text(), 'My Button')]")
button.click()
You were almost there. Show more results isn't the innerText but the value of the value attribute.
Solution
To click on the element with text as Show more results 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, "input[value='Show more results']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#value='Show more results']"))).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

Selenium Checkbox Selection

I'm trying to use selenium to click on a checkbox, as is depicted in the HTML of the picture. The think is, the checkbox input tag is the same for all four options, and they are distinguished only through the id="country_england" ... . I'm not sure how I can write code to select the england box and click it. Any help appreciated.
HTML Code:
use xpath:
//b[#id="country_england"]/../input
This finds the p tag that has that ID and then goes to the parent element and navigates back to input element
you can also use:
//b[#id="country_england"]/preceding-sibling::input
This finds the preceding element of p tag that has tag input
Code would be :
webD.get("https://icem.data-archive.ac.uk/#step1")
WebDriverWait(webD, 20).until(EC.element_to_be_clickable(
(By.XPATH, '//*[#value="1861"]'))).click()
WebDriverWait(webD, 20).until(EC.element_to_be_clickable(
(By.XPATH, '//b[#id = "country_england"]/preceding-sibling::input'))).click()
The desired element is a Angular element. So 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 preceding:
driver.get("https://icem.data-archive.ac.uk/#step1")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#value='1851']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//b[#class='ng-binding' and #id='country_england']//preceding::input[1]"))).click()
Using XPATH and preceding-sibling:
driver.get("https://icem.data-archive.ac.uk/#step1")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#value='1851']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//b[#class='ng-binding' and #id='country_england']//preceding-sibling::input[1]"))).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
Browser Snapshot:
Update

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 the "Mail" button on the yahoo page using Selenium and Python

I got a BUTTON variable for those buttons:
GoMailsBTN = browser.find_element_by_class_name("D(ib) Fz(14px) Fw(b) Lh(24px) Pstart(38px)")
GoMailsBTN.click()
and there is not like any id you can go check it out by yourself if you want!
This is the error that pops out
selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: An invalid or illegal selector was specified
Anyone got any idea why this is?
Snapshot of the button:
The class name is dynamic and can change, in this case, you can use xpath:
browser.find_element_by_xpath("//li//a[contains(#href ,'mail') and not(#id)]")
The desired element is a JavaScript enabled element so to click on the element you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
driver.get('https://in.yahoo.com/?p=us')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a#header-mail-button span"))).click()
Using XPATH:
driver.get('https://in.yahoo.com/?p=us')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#id='header-mail-button']//span"))).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
Browser Snapshot:

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