i have a button like this
<button data-control-name="more_comments" id="ember1181" class="comments-comments-list__load-more-comments-button artdeco-button artdeco-button--muted artdeco-button--1 artdeco-button--tertiary ember-view" style="" xpath="1"><!---->
<span class="artdeco-button__text" style="">
Load more comments
</span></button>
and it is loaded with ajax.
my code last code is
browser.implicitly_wait(50)
browser.execute_script("document.getElementsByClassName('comments-comments-list__load-more-comments-button artdeco-button artdeco-button--muted artdeco-button--1 artdeco-button--tertiary ember-view')[1].click();")
i am getting this error oven and over
selenium.common.exceptions.JavascriptException: Message: javascript error: Cannot read property 'scrollIntoView' of undefined
i have tried
implicit_wait
WebElementWait
selection by css selector
by xpath
but still no improvements
Thanks in advance
So Id's are dynamic so you can use below solution to handle 1st comment section from your web page:
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.XPATH, "/(//button[#data-control-name='comment'])[1]"))).click()
wait.until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Load more comments']"))).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
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 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
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()
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()
I tried typing 'abc' in the first block of id and 'cdef' in the second block of password.
However, the error code at the bottom comes up.
from selenium import webdriver
driver.get('http://sugang.korea.ac.kr')
I added an implicit wait to prevent the code from executing before the page fully loads.
driver.implicitly_wait(30)
Code for adding username and password is as below:
driver.find_element_by_name('id').send_keys('abc')
driver.find_element_by_name('pw').send_keys('cdef')
But I am getting the below error:
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"id","selector":"id"}
The 'No Such Element' exception usually comes when web driver can't see the element you are trying to perform an action on.
Reasons can be:
your ID or Name or XPath or CssSelector can be wrong.
Your element might be inside an an iframe so that web driver can't see or detect it. Switch to an iframe through Selenium and python
Your element is taking time to appear on the UI, so you can use an explicit wait to solve this. See 5. Waits
The username and password fields are within an frame, so you have to:
Induce WebDriverWait for the desired frame to be available and switch to it.
Induce WebDriverWait for the desired element to be clickable.
You can use the following solution:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver = webdriver.Firefox(executable_path=r'C:\\Utility\\BrowserDrivers\\geckodriver.exe')
driver.get("http://sugang.korea.ac.kr")
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.NAME,"firstF")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.input_login[name='id']"))).send_keys('abc')
driver.find_element_by_css_selector("input.input_login[name='pw']").send_keys("cdef")
Browser Snapshot:
Add explicitly wait
from selenium.webdriver.support import expected_conditions as EC
userNameElement= WebDriverWait(driver, 2).until(
EC.presence_of_element_located((By.NAME, "id"))
userNameElement.send_keys('abc')
pwdElement= WebDriverWait(driver, 2).until(
EC.presence_of_element_located((By.NAME, "pwd"))
pwdElement.send_keys('cdef')
Here, I am expecting that your locators are correct.
It is in a frame which you need to switch to first. Also, use ids where possible as they are faster.
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
url ="http://sugang.korea.ac.kr"
driver = webdriver.Chrome()
driver.get(url)
WebDriverWait(driver,5).until(EC.visibility_of_element_located((By.CSS_SELECTOR,'[name=firstF]')))
driver.switch_to.frame(driver.find_element_by_css_selector('[name=firstF]'))
WebDriverWait(driver,5).until(EC.visibility_of_element_located((By.ID,'id'))).send_keys('abc')
driver.find_element_by_id('pw').send_keys('def')
driver.find_element_by_id('loginButton').click()
The site you are trying to access does not have an element with a tag name id. Examine the site carefully.
<input name="id">
If the input has an id value, try this;
driver.find_element_by_id("id")
Example Use:
HTML:
<div class="form-group">
<input class="form-control" name="username">
</div>
<div class="form-group">
<input class="form-control" name="password" type="password">
</div>
<button id="btn-login" type="submit">Enter</button>
Python:
username = driver.find_element_by_name("username")
password = driver.find_element_by_name("password")
username.send_keys("your_username")
password.send_keys("your_password")
driver.find_element_by_id("btn-login").click()