I'm trying to perform a click on different div class TopBox. i tried the codes below but i don't get the click performed :
driver.find_element_by_css_selector('#home > div > div.row.topBoxs > div.col-xs-12.col-lg-10 > div > div:nth-child(1) > div').click()
and also :
driver.find_element_by_xpath('//*[#id="home"]/div/div[2]/div[1]/div/div[1]').click()
Below is the snapshot of the code of the box "mes posts" as exemple and the other boxes.
Try with xpath instead of css_selector.
driver.find_element_by_xpath('xpath_of_your_Div').click()
Here is what you have to do.
# get number of the toolboxes first
toolBoxes = len(driver.find_element_by_css_selector("div.boxs div[class^='topBox ']"))
# now you can click on each of them
for boxNumber in range(toolBoxes):
# you can use either xpath/css to get the nth box and click()
driver.find_element_by_xpath("(//div[#class='boxs']/div[starts-with(#class,'topBox ')])[" + str(boxNumber+1) + "]").click()
# waiting here so that you can see the element is clicked (optional)
time.sleep(2)
Firstly, check if you are able to locate the element. If it is not then following line should be throwing error:
WebElement theButton = driver.find_element_by_xpath('//div[#class='boxs']/div[1]/div');
alternatively pls try this xpath as well
'//div[#class='boxs']/div[1]/div/div/p/span'
Wait for the element till its clickable
wait = WebDriverWait(driver, 15)
wait.until(EC.element_to_be_clickable((By.XPATH, my_xpath)))
Then try clicking the element with .click()
Please let me know your observations which would help troubleshoot
Try this code with this css selector:
all_boxes = driver.find_elements_by_css_selector("div.content-box span")
for box_i in range(len(all_boxes)):
driver.find_elements_by_css_selector("div.content-box span")[box_i].click()
If you are getting an error, please let me know, what it is.
Related
i've written the below lines of code:
elem = driver.find_elements(By.XPATH, "//span[#class='drop__expand']")
for i in elem:
i.click()
and i get the below error:
selenium.common.exceptions.ElementClickInterceptedException: Message: Element <span class="drop__expand"> is not clickable at point (112,20) because another element <div class="wrapper"> obscures it
i tried that without any result:
driver.find_elements(By.XPATH, "//div[#class='wrapper']").click()
elem = driver.find_elements(By.XPATH, "//span[#class='drop__expand']")
for i in elem:
i.click()
how can i handle this?
There are two functions:
find_elements() with s at the end - to get list with all matching elements - even if there is one element or there is no elements.
find_element() without s at the end - to get only first matching elements.
So to click element you may need second function (without s)
driver.find_element(By.XPATH, "//div[#class='wrapper']").click()
or you have to get first element from list when you use first function (with s)
driver.find_elements(By.XPATH, "//div[#class='wrapper']")[0].click()
or use for-loop
for item in driver.find_elements(By.XPATH, "//div[#class='wrapper']"):
item.click()
But wrapper may not be clickable - if it is some popup message then you may need to find button on this wrapper. But you didn't show URL for this page so only you have access to full HTML to check if it has button and find xpath for this button. And here I can't help.
You may also try to use JavaScript to click hiddent element(s) and maybe it will work.
Something like this
elem = driver.find_elements(By.XPATH, "//span[#class='drop__expand']")
for i in elem:
driver.execute_script("arguments[0].click()", i)
But all this is only guess because you didn't show url for this page and we can't test it.
with selenium, i'm trying to click an element but not working with this element,
the page is here page (username/password :admin/admin)
wait2 = WebDriverWait(driver, 10)
element = wait2.until(EC.element_to_be_clickable((By.XPATH, '//*[#id="operate2a5a0448a8bf44a8898ec13e95b152fc"]/div/div[2]')))
element.click()
i tried this on other element in the same page and got no problem
no idea why not working on this element
operate2a5a0448a8bf44a8898ec13e95b152fc seems to be dynamically created id.
The simplest way to access this element is with text based XPath locator:
wait2.until(EC.element_to_be_clickable((By.XPATH, "//div[contains(text(),'Entry Registration')]"))).click()
you can try with below code as well :
//img[contains(#src,'registration')]/..
in code :
wait2 = WebDriverWait(driver, 10)
element = wait2.until(EC.element_to_be_clickable((By.XPATH, "//img[contains(#src,'registration')]/..")))
element.click()
my code :
react_on_video = (input("React: ")).upper()
video_link = "https://www.youtube.com/watch?v=blablabla"
(After login with my gmail account)
driver.get(video_link)
driver.execute_script("window.scrollTo(0, 300);")
time.sleep(1)
if react_on_video == "LIKE":
Like_button_Full_xpath = "/html/body/ytd-app/div/ytd-page-manager/ytd-watch-
flexy/div[5]/div[1]/div/div[8]/div[2]/ytd-video-primary-info-
renderer/div/div/div[3]/div/ytd-menu-renderer/div[1]/ytd-toggle-button-renderer[1]/a/yt-
icon-button/button/yt-icon"
driver.find_element_by_xpath(Like_button_Full_xpath).click()
if react_on_video == "DISLIKE":
Dislike_button_Full_xpath = "/html/body/ytd-app/div/ytd-page-manager/ytd-watch-
flexy/div[5]/div[1]/div/div[8]/div[2]/ytd-video-primary-info-
renderer/div/div/div[3]/div/ytd-menu-renderer/div[1]/ytd-toggle-button-renderer[2]/a/yt-
icon-button/button/yt-icon"
driver.find_element_by_xpath(Dislike_button_Full_xpath).click()
But every time it shows No such element exception!
[selenium.common.exceptions.NoSuchElementException: Message: no such element:]
I've tried with other selectors and xpaths; but none of them worked! Is there any permanent solution? Because once I was able to click on the buttons with this xpath. I had to change xpath multiple times.
Like_button_xpath : '//[#id="top-level-buttons"]/ytd-toggle-button-renderer1/a'
Dislike_button_xpath : '//[#id="top-level-buttons"]/ytd-toggle-button-renderer[2]/a'
I found Solution. Try xPath
Like_button_Full_xpath = "//div[3]/div/ytd-menu-renderer/div/ytd-toggle-button-renderer/a/yt-icon-button/button/yt-icon"
Tell me if it work.
Instead of using xpath, I would use a css selector. I can't guarantee that this selector will be unique without seeing the full HTML, but I would try finding the "Like" button like this:
driver.find_element_by_css_selector('yt-icon.style-scope.ytd-toggle-button-renderer')
I can't seem to find the right Xpath for what I am trying to do:
the element I am trying to click on appears to be:
//*[#id="react-autowhatever-1--item-0"]/span/div[2]
but when run this code:
wait = WebDriverWait(imdbScrape.driver, 10)
wait.until(ec.element_to_be_clickable((By.XPATH,
'//*[#id="suggestion-search"]'))).send_keys("the"
+ "fifth element")
searchBar = wait.until(ec.element_to_be_clickable((By.XPATH,
'//*[#id="react-autowhatever-1--item-0"]/span/div[2]')))
searchBar.location_once_scrolled_into_view
urlToScrape = searchBar.get_attribute("href")
print(urlToScrape)
I get "None" as the result, I am assuming it because when I look on the page I don't see any "href" tags but I am wondering how I can get the first selection's link address
Any help would be appreciated
Thanks,
I am new to selenium here. I am trying to use selenium to click a 'more' button to expand the review section everytime after refreshing the page.
The website is TripAdvisor. The logic of more button is, as long as you click on the first more button, it will automatically expand all the review sections for you. In other words, you just need to click on the first 'more' button.
All buttons have a similar class name. An example is like taLnk.hvrIE6.tr415411081.moreLink.ulBlueLinks. Only the numbers part changes everytime.
The full element look like this:
<span class="taLnk hvrIE6 tr413756996 moreLink ulBlueLinks" onclick=" var options = {
flow: 'CORE_COMBINED',
pid: 39415,
onSuccess: function() { ta.util.cookie.setPIDCookie(2247); ta.call('ta.servlet.Reviews.expandReviews', {type: 'dummy'}, ta.id('review_413756996'), 'review_413756996', '1', 2247);; window.location.hash = 'review_413756996'; }
};
ta.call('ta.registration.RegOverlay.show', {type: 'dummy'}, ta.id('review_413756996'), options);
return false;
">
More </span>
I have tried several ways to get the button click. But since it is an onclick event wrapped by span, I can't successfully get it clicked.
My last version looks like this:
driver = webdriver.Firefox()
driver.get(newurl)
page_source = driver.page_source
soup = BeautifulSoup(page_source)
moreID = soup.find("span", class_=re.compile(r'.*\bmoreLink\b.*'))['class']
moreID = '.'.join(moreID[0:(len(moreID)+1)])
moreButton = 'span.' + moreID
button = driver.find_element_by_css_selector(moreButton)
button.click()
time.sleep(10)
However, I keep getting the error message like this:
WebDriverException: Message: Element is not clickable at point (318.5,
7.100006103515625). Other element would receive the click....
Can you advise me on how to fix the problem? Any help will be appreciated!
WebDriverException: Message: Element is not clickable at point (318.5, 7.100006103515625). Other element would receive the click....
This error to be occur when element is not in the view port and selenium couldn't click due to some other overlay element on it. In this case you should try one of these following solution :-
You can try using ActionChains to reach that element before click as below :-
from selenium.webdriver.common.action_chains import ActionChains
button = driver.find_element_by_css_selector(moreButton)
ActionChains(button).move_to_element(element).click().perform()
You can try using execute_script() to reach that element before click as :-
driver.execute_script("arguments[0].scrollIntoView(true)", button)
button.click()
You can try using JavaScript::click() with execute_script() but this JavaScript::click() defeats the purpose of the test. First because it doesn't generate all the events like a real click (focus, blur, mousedown, mouseup...) and second because it doesn't guarantee that a real user can interact with the element. But to get rid from this issues you can consider it as an alternate solution.
driver.execute_script("arguments[0].click()", button)
Note:- Before using these options make sure you're trying to interact with correct element using with correct locator, otherwise WebElement.click() would work well after wait until element visible and clickable using WebDriverWait.
Try using an ActionChains:
from selenium.webdriver.common.action_chains import ActionChains
# Your existing code here
# Minus the `button.click()` line
ActionChains(driver).move_to_element(button).click().perform()
I have used this technique when I need to click on a <div> or a <span> element, rather than an actual button or link.