Python Selenium select div class with blanks - python

I've got a problem while trying to select a div tag class that has some white spaces in it.
This is the structure of page:
<div class="sadasd-dashboardtab even asdasd-syndicating_from_my_file" id="124121_1540012412412414">
<div id="124121_154006585856856858">
<span class="label">Syndicating From My File</span>
<button class="column-dropdown" title="Click for more tab options"></button>
</div>
</div>
This are my tries of code for this part:
#syndicating_button = driver.find_element_by_xpath("//span[text()='Syndicating From My File']")
#syndicating_button = driver.find_element_by_xpath("//div[#class='yui3-dashboardtab even s-tab-syndicating_from_my_site']")
syndicating_button = driver.find_element_by_css_selector("div.yui3-dashboardtab.even.s-tab-syndicating_from_my_site")
syndicating_button.click()

Your issue has nothing to do with "blanks"/"whitespaces" as your selectors should work well... if element is present in DOM. Try to wait until element appears in DOM and becomes clickable:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC
wait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Syndicating From My Site']"))).click()

Related

How to click on checkbox filter with selenium Python?

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()

Selenium unable to locate and click a button

I have tried to make my script click the checkout button with WebDriverWait, XPath, Class, but it doesn't work anyways :
HTML
<div class="confirm-container row">
:before
<div class="col-xs-12">
<button class="btn btn-primary">
<span>Effettua l'ordine</span>
</button>
</div>
::after
</div>
1st try
var = '//*[#id="purchase-app"]/div/div[4]/div[1]/div[2]/div[5]/div/div/button'
WebDriverWait(web, 50).until(EC.element_to_be_clickable((By.XPATH, var)))
web.find_element_by_xpath(var).click()
2nd try
web.find_element_by_class_name('btn btn-primary').click()
3rd try
ActionChains(web).click(web.find_element_by_class_name('btn btn-primary')).perform()
When you are using Explicit wait, try to have a relative xpath :
1st try issue can be resolved by the below code :
WebDriverWait(web, 10).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Effettua l'ordine']/.."))).click()
2nd try web.find_element_by_class_name('btn btn-primary').click() - class name does not accept spaces so, use css selector instead :
web.find_element_by_css_selector('button.btn.btn-primary').click()
same issue with 3rd try.
Update 1 :
The issue is that, the button you are looking is in iframe so you need to switch over it before interaction :
Code :
wait = WebDriverWait(driver, 10)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[src^='/store/purchase?namespace']")))
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn.btn-primary"))).click()
Imports :
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
You can try this
from selenium.webdriver import ActionChains
ActionChains(browser).click(element).perform()
while element refer to anything between the start tag and end tag.
as stated in the answer here: https://stackoverflow.com/a/61036237/11884764

How to use derive an X-Path from a specific HTML attribute to use with Selenium

Background:
I have the following HTML code that I am trying to get a XPATH to use with Selenium
<div class="btn-group">
<a type="button" class="btn btn-primary btn-user" onclick="AiD('182030801')" href="/download.pdf?id=182030801&h=917901e6659ad5eb53970aecf687b53e&u=cache&ext=pdf" target="_blank" style="border-top-left-radius: 3px;border-bottom-left-radius: 3px;">
<i class="fas fa-cloud-download-alt" aria-hidden="true" style="margin-right: 9px;margin-left: 2px;font-size: 25px;vertical-align: middle;color: #119802;"></i>Download ( PDF )
</a>
[...]
</div>
Code:
What have I tried to do with Python's Selenium is the following, however I cannot quiet get it to work without Python throwing an error:
browser.find_element(By.XPATH, "//div[#class='btn-group']/a").click()
browser.find_element(By.XPATH, "//div[#class='btn tn-primary btn-user']").click()
Error:
When I run the above snippet of code, the following error is produced and script crashes thereafter:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//div[#class='btn tn-primary btn-user']"}
Problem:
The error states "no such element", however the element clearly exists "class="btn btn-primary btn-user""
Question:
How can I use Seleniums XPATH to "see" class="btn btn-primary btn-user" and click it to download a PDF?
Link:
PDFDrive
USe Xpath //div/a[#class='btn btn-primary btn-user']
Make sure the intended element is not under an iFrame. If it is then first you need to switch into that iFrame and then have to perform the action
Make sure you are using proper synchrnization and your element is ready. Introduce explicit wait as below:
driver.get('https://www.pdfdrive.com/querying-xml-xquery-xpath-and-sqlxml-in-context-d38665640.html')
WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[#class='btn-group']/a"))).click()
time.sleep(10) // it doesn't recommanded to put hardcoded wait but for debugging purpose you can check
Import below packages for this:
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
You are using "div" tag but I can see respective class = btn btn-primary btn-user is inside "a" tag
try below code :
browser.find_element(By.XPATH, "//a[#class='btn tn-primary btn-user']").click()
Or you can go with another locator as well.
CSS : a.btn.btn-primarybtn-user
So you need to wait for the element to be clickable. Triggered the download.
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
options = Options()
driver = webdriver.Chrome(ChromeDriverManager().install(),options=options)
url = 'https://www.pdfdrive.com/querying-xml-xquery-xpath-and-sqlxml-in-context-d38665640.html'
driver.get(url)
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,'a.btn.btn-primary.btn-user'))).click()

Python Selenium - get element with a date-rolename

I have the following HTML markup:
<div data-request-type="person" class="_entry _line _e _selected" id="c1055e27-0cfe-4f93-8bea-28a3421d842e" data-rolename="Member" data-isoptional="true">
<div class="_subindicator _gray">&nbsp</div>
<div class="_removePers"> </div>
<div class="_voluntaryPers"> </div>
<div class="_text">Member</div>
</div>
I have to click on the first <div> using .click().
But now I don't know how to find this with selenium. I already tried it with XPATH, but I have several elements with different IDs on this page. And the IDs are always regenerated. This does not work.
Does anyone have an idea?
I tried it with a lot of solutions...but nothing works.
My last one - to take a inner div with class _text
getAllMembers = browser.find_element_by_css_selector('td._text').get_attribute('innerHTML')
Please help - how can I do this with Selenium? :-)
Try below xpath :
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.XPATH, "//div[#class='_entry _line _e _selected'][#data-rolename='Member']"))).click()
Note : please add below imports to your solution
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
To select and click on a element by its attribute, you can use:
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
driver = webdriver.Firefox()
wait = WebDriverWait(driver, 10)
driver.get("https://site.tld")
xpath = "//div[#data-rolename='Member']"
el = wait.until(EC.element_to_be_clickable((By.XPATH, xpath)))
el.click()

Locate text which loads dynamically Python Selenium WebDriver

My problem is that I am trying to get the text of the element, which fills up with HTML content dynamically. Here is HTML source code. The last div:
<div data-bind="with: dataForRatingPopup"></div>
Fills with HTML code when the mouse enters the first div:
<div class="star-rating-wrap" data-bind="event: { mouseenter: mouseEnter, mouseleave: mouseLeave, click: click }, css: { 'has-supplier': emex.context.userIsInOptovikInterface }">
<div class="star-rating rating-6" data-bind="css: css"></div>
<div data-bind="with: dataForRatingPopup"></div>
</div>
How can I get the text of this element using Selenium WebDriver?
You can use webdriverwait function for this
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(driver, 100).until(
EC.text_to_be_present_in_element((By.XPATH, '//div[contains(#data-bind, "dataForRatingPopup")]'), "txt you want to match")
)
Above code will wait up to 100 seconds to find a div (whose #data-bind attribute contains the value "dataForRatingPopup") which contains the text "txt you want to match"
Useful links :
https://christopher.su/2015/selenium-chromedriver-ubuntu/ (You can skip pyvirtualdisplay usage in that)
http://selenium-python.readthedocs.io/getting-started.html#simple-usage

Categories