I am new to coding Selenium in Python and, I have been trying to find this button through name, id, xpath, and then click on it but nothing has worked.
The issue is, I cannot find the button because the xpath is constantly changing, and the ID name is just "button", which locating it that way would not work either because there are a lot of other buttons on the page. How can I located the element?
Here is the sites HTML:
<ul data-componentname="gender">
<li id="b27296be-e8da-4d5a-acb6-d1674bf88568" class="">
<input type="button">
<span>Male</span>
</li>
<li id="32bf7074-6b69-41bb-9869-cf71ac42686f" class="">
<input type="button">
<span>Female</span>
</li>
Here is what I have tried:
clickGender = browser.find_element_by_xpath("b27296be-e8da-4d5a-acb6-d1674bf88568")
Any help is greatly appreciated.
As per the HTML you have shared to find the dynamic button corresponding to the text as Male or Female and then click on it you have to induce WebDriverWait for the element to be clickable and you can use the following solution:
Male:
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//ul[#data-componentname='gender']//li//span[contains(.,'Male')]//preceding::input[1]"))).click()
Female:
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//ul[#data-componentname='gender']//li//span[contains(.,'Female')]//preceding::input[1]"))).click()
Note: You need 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
You need to use find_element_by_id:
clickGender = browser.find_element_by_id("b27296be-e8da-4d5a-acb6-d1674bf88568")
Your XPATH should be this (assuming you want to select the 'Male' button):
//li[span/text()='Male']
To find the button with text that contains "Male", use this:
driver.find_element_by_xpath("//li[contains(string(), 'Male')]/input[contains(#type,'button')]")
To find the button with text that contains "Female", use this:
driver.find_element_by_xpath("//li[contains(string(), 'Female')]/input[contains(#type,'button')]")
Related
I'm trying to click on a checkbox filter on a website with selenium python. This is a part of its HTML code linked to one of the checkbox options.
<div class="shopee-checkbox" bis_skin_checked="1">
<label class="shopee-checkbox__control">
<input type="checkbox" name="" value="Jabodetabek">
<div class="shopee-checkbox__box" bis_skin_checked="1">
<i> </i>
</div>
<span class="shopee-checkbox__label">Jabodetabek</span>
</label>
</div>
I tried following, but it didn't work.
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get('https://shopee.co.id/search?keyword=baju%20laki-laki')
time.sleep(5)
driver.find_element(By.XPATH, "//input[#value='JABODETABEK']").click()
I read the answers to similar questions, mostly they suggest using 'id' and 'name' to find the element. However, in this input tag, there are only 'type' and 'value'.
Here is one way to select that checkbox:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
[...]
wait = WebDriverWait(driver, 25)
[..]
wait.until(EC.element_to_be_clickable((By.XPATH, '//span[text()="Jabodetabek"]'))).click()
print('clicked on Jabodetabek')
Selenium documentation can be found here.
#value for the checkbox in the xpath is incorrect. The selenium web locator techniques are case sensitive. So make sure to use exact values while identifying objects. Changing your xpath to below would fix the issue.
driver.find_element(By.XPATH, "//input[#value='Jabodetabek']").click()
I'm using python and Selenium to scrape some search results in LinkedIn but I'm having trouble finding the people button on the top left in order to filter my results to have only people. I noticed that the ID of the button is dynamic so I tried different ways to find the buttons, all of which have failed. Here is the HTML code of the button:
<div class="peek-carousel js-slideshow">
<ul class="peek-carousel__slides js-list">
<li class="mr2">
<button aria-checked="false" role="radio" aria-label="People" id="ember69" class="artdeco-pill artdeco-pill--slate artdeco-pill--2 artdeco-pill--choice ember-view search-reusables__filter-pill-button" type="button"> People </button>
and here are my three attempts (python code):
1) people_button = driver.find_element_by_css_selector("#people + button")
2) people_button = driver.find_element_by_xpath("//button[#aria-label='People']")
3) people_button = driver.find_element_by_xpath("//button[#aria-label='People'][#type='button']")
I'm having also the same problem for the next button on the bottom right to go to the next page here is the HTML code of the button:
<span class="artdeco-button__text">
Next
</span>
The first locator doesn't work because there is no element with an ID "people" on the page. The second and third work fine for me. I've tested them in the dev tools in Chrome using $$() for CSS selector and $x() for XPath. In this case, $x("//button[#aria-label='People']") returns a single element. My guess is that you need to add a wait.
I would use a CSS selector in this case because they are faster and better supported, e.g. "button[aria-label='People']". But, since you used XPath this should work
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
new WebDriverWait(driver, 10).until(EC.element_to_be_clickable(By.XPath("//button[#aria-label='People']"))).click()
going to https://www.google.com/search?q=tennis a google one box comes up for scores, I'm trying to click on the tabs and (Women's Singles, Men's Doubles etc) and having no luck. I've tried all methods, xpath, classname, partial link text, css selector.
any help would be appreciated!!!
The desired element is a JavaScript enabled 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:
Clicking Women's Singles:
driver.get('https://www.google.com/search?q=tennis')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[#jsslot]//span//span[contains(., 'Women') and contains(., 'Singles')]"))).click()
Clicking Men's Doubles:
driver.get('https://www.google.com/search?q=tennis')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[#jsslot]//span//span[contains(., 'Men') and contains(., 'Doubles')]"))).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
I always use this method to set the XPath in Selenium based Python projects.
Click F12 after opening the link
In Elements, Click Ctrl + F. Now search some unique term in the page. I gave Men's Singles and this snippet came up.
<div class="SVWlSe FZzi2e" jsslot="">
<span jsslot="">
<div aria-controls="_dtAkYIqaMLraz7sPnrW54A435_0"
id="_dtAkYIqaMLraz7sPnrW54A434_0">
<span>
<span>Men's Singles</span>
</span>
</div>
</span>
</div>
Now right-click the snippet to get the XPath format or use the XPath Syntax. Here, the XPath is //div[#class='SVWlSe FZzi2e']. To find whether the attribute value is unique, I would suggest you to search within elements again with the value.
Notice that I used the class attribute instead of the aria-controls and id attributes since id and aria-controls changed over time.
I am starting with Python - Selenium and I cannot target the element in login website. I tried many options of targeting this button but no options works (class name, css selector, id, name..). When I skip this step and get on the next page manually by exact url the finding, focusing, sending keys to element and clicking on the "Next" button is no problem but this "welcome" login button cannot target totally.
Using function "driver.find_element_by_XXX".
For example:
"driver.find_element_by_class_name("login-box-actions").click()"
Please, where I am making a mistake?
Many thanks,
regards David
<div class="login-box-actions">
<a style="cursor: pointer" ng-click="confirmLogin()" class="btn btn-primary btn-block btn-flat ng-binding">Login</a>
</div>
It may fail because the element could not found.
For that, you have to wait for the element to be load and clickable.
Refer to the following example, wait for the element to be clickable.
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(driver, 20).until(EC.element_to_be_clickable(
(By.CSS_SELECTOR, "btn btn-primary btn-block btn-flat ng-binding"))).click()
You can try finding the element by using different identifiers. Selenium supports:
find_element_by_id
find_element_by_name
find_element_by_xpath
find_element_by_link_text
find_element_by_partial_link_text
find_element_by_tag_name
find_element_by_class_name
find_element_by_css_selector
You need to click into the <a> element instead of the div:
login_button = driver.find_element_by_xpath("//a[text()='Login']")
login_button.click()
I have a table column with four links in the same cell:
<td>
<a href="", onclick=showPricing('1234567','P', 'xyz123456', )>pdf,</a>
<a href="", onclick=showPricing('1234567','C', 'xyz123456', )>csv,</a>
<a href="", onclick=showPricing('1234567','X', 'xyz123456', )>xls,</a>
<a href="", onclick=showPricing('1234567','XP', 'xyz123456', )>xlsp</a>
</td>
I want to click the fourth one link text "xlsp", but not able to.
What I tried:
1) driver.find_element_by_xpath('//a[contains(#onclick, "xyz123456")]').click()
this of course clicks the first one: pdf
2) driver.find_element_by_xpath('//a[contains(#onclick, "xyz123456")][contains(text(), "xlsp")]').click()
this picks the fourth one, but error out with
"....errorElement could not be scrolled into view"
3) driver.find_elements_by_xpath('//a[contains(#onclick, "xyz123456")]')[3].click()
this returns the same error as 2)
Any suggestions are appreciated.
To click the fourth forth link with text as xlsp you can use either of the following solutions:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "td a[onclick*='XP']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(#onclick,'XP') and contains(.,'xlsp')]"))).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
I tested multiple options, including #DebanjanB's suggestion, and think this is the most reliable solution:
driver.execute_script(dr.find_element_by_xpath('//a[contains(#onclick, "xyz123456")][contains(#onclick, "XP")]').get_attribute("onclick"))