This question already has answers here:
Element MyElement is not clickable at point (x, y)... Other element would receive the click
(5 answers)
Closed 3 years ago.
I am trying to scrape this website with selenium on python I keep getting this error every time I try to click on a dropdown button that is inside a div tag can some help me please the error is 'Element is not clickable at point (1341, 240)' below is the website
'https://tennisinsight.com/player/56330/andrea-gamiz/'
if you scroll to the bottom of the page, I am trying to click on the duration dropdown options in the match stats sections. below is my code so far
driver = webdriver.Chrome()
driver.maximize_window()
wait = WebDriverWait(driver, 5)
small_wait = WebDriverWait(driver, 5)
driver.execute_script('window.open("https://tennisinsight.com/player/56330/andrea-gamiz/","_self")')
driver.execute_script("document.body.style.zoom='75%'")
from selenium.webdriver.common.keys import Keys
html = driver.find_element_by_tag_name('html')
html.send_keys(Keys.END)
time.sleep(3)
element = wait.until(EC.element_to_be_clickable((By.XPATH, ' //*[#id="matchStatsDuration"]')))
element.click()
Here is the simple approach that I would follow to select the items from this list.
# select Month from the list.
element = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH, "//select[#id='matchStatsDuration']/option[.='Month']")))
element.location_once_scrolled_into_view
element.click()
By this way I don't have to worry about the overlaying top menu, which is obstructing the click on the list element.
Related
How to scroll through an element using Selenium?
It's been a while since I've used selenium, but doing something like this should scroll until the desired element is in view using JavaScript.
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("your-site.com")
# Find element by ID or some other method
element = driver.find_element_by_id("id_of_element")
# Run JavaScript to scroll until the element is in view
driver.execute_script("arguments[0].scrollIntoView(true);", element);
I am trying to get search results from yahoo search using python - selenium and bs4. I have been able to get the links successfuly but I am not able to click the button at the bottom to go to the next page. I tried one way, but it could't identify after the second page.
Here is the link:
https://in.search.yahoo.com/search;_ylt=AwrwSY6ratRgKEcA0Bm6HAx.;_ylc=X1MDMjExNDcyMzAwMgRfcgMyBGZyAwRmcjIDc2ItdG9wLXNlYXJjaARncHJpZANidkhMeWFsMlJuLnZFX1ZVRk15LlBBBG5fcnNsdAMwBG5fc3VnZwMxMARvcmlnaW4DaW4uc2VhcmNoLnlhaG9vLmNvbQRwb3MDMARwcXN0cgMEcHFzdHJsAzAEcXN0cmwDMTQEcXVlcnkDc3RhY2slMjBvdmVyZmxvdwR0X3N0bXADMTYyNDUzMzY3OA--?p=stack+overflow&fr=sfp&iscqry=&fr2=sb-top-search
This is what im doing to get data from page but need to put in a loop which changes pages:
page = BeautifulSoup(driver.page_source, 'lxml')
lnks = page.find('div', {'id': 'web'}).find_all('a', href = True)
for i in lnks:
print(i['href'])
You don't need to scroll down to the bottom. The next button is accessible without scrolling. Suppose you want to navigate 10 pages. The python script can be like this:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
driver=webdriver.Chrome()
driver.get('Yahoo Search URL')
# Let's create a loop containing the XPath for next button
# As well as waiting for the next button to be clickable.
for i in range(10):
WebDriverWait(driver, 5).until(EC.element_to_be_clickable(By.XPATH, '//a[#class="next"]'))
navigate = driver.find_element_by_xpath('//a[#class="next"]').click()
The next page button is on the bottom of the page so you first need to scroll to that element and then click it. Like this:
from selenium.webdriver.common.action_chains import ActionChains
actions = ActionChains(driver)
next_page_btn = driver.find_element_by_css_selector("a.next")
actions.move_to_element(next_page_btn).build().perform()
time.sleep(0.5)
next_page_btn.click()
I am trying to click on the "Next page" in Python-Selenium. The element and its path are seen, the buttom is being clicked but after clicking an error is shown:
"StaleElementReferenceException:stale element reference: element is not attached to the page document"
My code so far:
element = WebDriverWait(driver, 20).until(EC.presence_of_element_located(\
(By.XPATH, butn)))
print (element.is_enabled())
while True and element.is_enabled()==True:
driver.find_element_by_xpath(butn).click()
The error is one element.is_enabled()==True after clicking
Can someone help?
When you search elements in Selenium then it doesn't keep full objects but only references to objects in DOM in browser. And when you click then browser creates new DOM and old references are incorrect and you have to find elements again.
Something like this
# find first time
element = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, butn)))
print(element.is_enabled())
while element.is_enabled():
driver.find_element_by_xpath(butn).click()
# find again after changes in DOM
element = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, butn)))
print(element.is_enabled())
Basically, I am trying to create a bot that can find an element on a page with a given raw_input text, finding the button, and clicking it. The purpose of this is to click on links (buttons) that may not be on the web page yet, but will appear after the site refreshes. Because of this, I cannot find elements by XPATH, because the XPATH will be unknown until the second the link becomes available. So, my question is: is there a way for Selenium to find a button based on text and click it? Here is some of my code:
key1 = raw_input('Enter the first keyword: ')
key2 = raw_input('Enter the second keyword: ')
key3 = raw_input('Enter the third keyword: ')
...
elem1 =driver.find_elements_by_xpath("//*[contains(text(), key1)]")
if elem1.is_displayed():
elem1.click()
else:
print "Cannot find element to click. Retrying..."
driver.refresh()
I need the program to find the text based on the keywords, and then click the button / link with these keywords in it. Is this possible? or only possible with XPATHs? The example code i gave with giving an elem has not been working.
thanks in advance
Ok first of all, the only one of selecting an element by text is sadly xpath, but do not worry, there is a workaround you could use waits
#Libraries (those comes with selenium, you don't have to install anything)
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
#Way
elem1 = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, '//*[contains(text(),
{})]'.format(key1))
)
elem1.click()
this will make it wait for the element to be clickable if the element is not clickable after 10 seconds it will raise an error if it gets clickable before that time it will return the element and break the wait automatically.
So I have been using selenium to open a webpage and wait for a specific element to be loaded. Once that's loaded, I'm finding an element within the first element that's already loaded and getting a value from that element. But, every time I run the code I get a StaleElementReferenceException on the line that says price = float(...). This is weird to me because it didn't crash on the line before which has the same xpath search. I'm very new to this, any help would be appreciated!
browser.get(url + page)
element = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID, "searchResultsTable")))
price_element = element.find_element_by_xpath("//div[#class='market_listing_row market_recent_listing_row market_listing_searchresult']")
price = float(price_element.find_element_by_xpath("//span[#style='color:white']").text[:1])