I am trying to loop clicks with Selenium,
the first click is working but the second gives error below:
selenium.common.exceptions.ElementNotInteractableException: Message: Element could not be scrolled into view
Here is the code:
def send_keys_dropdown(self,url):
bot = self.bot
bot.get(url)
elements = bot.find_elements_by_xpath("//a[#data-tag='globalize']")
for elem in elements:
class_of_element = elem.get_attribute("class")
if class_of_element == 'CsLinkButton':
elem.click()
time.sleep(5)
# close icon
bot.find_element_by_xpath("//a[#data-tag='cmdCancel']").click()
How I can click these elements inside loop? Thank you!
It seems after click action performed element may be inside scroll down or up window form.
following may help you please go through it.
after click action performed add following line.
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);",
elem);
here, "elem" is web element of your click button.
Related
I'm creating an Instagram bot but cannot figure out how to navigate to the next post.
Here is what I tried
#Attempt 1
next_button = driver.find_element_by_class_name('wpO6b ')
next_button.click()
#Attempt 2
_next = driver.find_element_by_class_name('coreSpriteRightPaginationArrow').click()
Neither of two worked and I get a NoSuchElementException or ElementClickInterceptedException . What corrections do I need to make here?
This is the button I'm trying to click(to get to the next post)
I have checked your class name coreSpriteRightPaginationArrow and i couldn't find any element with that exact class name. But I saw the class name partially. So it might help if you try with XPath contains as shown below.
//div[contains(#class,'coreSpriteRight')]
another xpath using class wpO6b. there are 10 elements with same class name so filtered using #aria-label='Next'
//button[#class='wpO6b ']//*[#aria-label='Next']
Try these and let me know if it works.
I have tried below code and it's clicking next button for 10 times
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
if __name__ == '__main__':
driver = webdriver.Chrome('/Users/yosuvaarulanthu/node_modules/chromedriver/lib/chromedriver/chromedriver') # Optional argument, if not specified will search path.
driver.maximize_window()
driver.implicitly_wait(15)
driver.get("https://www.instagram.com/instagram/");
time.sleep(2)
driver.find_element(By.XPATH,"//button[text()='Accept All']").click();
time.sleep(2)
#driver.find_element(By.XPATH,"//button[text()='Log in']").click();
driver.find_element(By.NAME,"username").send_keys('username')
driver.find_element(By.NAME,"password").send_keys('password')
driver.find_element(By.XPATH,"//div[text()='Log In']").click();
driver.find_element(By.XPATH,"//button[text()='Not now']").click();
driver.find_element(By.XPATH,"//button[text()='Not Now']").click();
#it open Instagram page and clicks 1st post and then it will click next post button for the specified range
driver.get("https://www.instagram.com/instagram/");
driver.find_element(By.XPATH,"//div[#class='v1Nh3 kIKUG _bz0w']").click();
for page in range(1,10):
driver.find_element(By.XPATH,"//button[#class='wpO6b ']//*[#aria-label='Next']" ).click();
time.sleep(2)
driver.quit()
As you can see, the next post right arrow button element locator is changing between the first post to other posts next page button.
In case of the first post you should use this locator:
//div[contains(#class,'coreSpriteRight')]
While for all the other posts you should use this locator
//a[contains(#class,'coreSpriteRight')]
The second element //a[contains(#class,'coreSpriteRight')] will also present on the first post page as well, however this element is not clickable there, it is enabled and can be clicked on non-first pages only.
As you can see on the picture below, the wp06b button is inside a lot of divs, in that case you might need to give Selenium that same path of divs to be able to access the button or give it a XPath.
It's not the most optimized but should work fine.
driver.find_element(By.XPATH("(.//*[normalize-space(text()) and normalize-space(.)='© 2022 Instagram from Meta'])[1]/following::*[name()='svg'][2]")).click()
Note that the XPath leads to a svg, so basically we are clicking on the svg element itself, not in the button.
I'm trying to scrape this site that has pagination. The problem I'm facing is having selenium locate the next button.
What I've tried:
next_button = driver.find_element_by_css_selector(
'ul[class="css-12ke8jn e65zztl0"] button[aria-label="Next"]').click()
and
page_amount = driver.find_element_by_css_selector(
'/html/body/div[1]/div[2]/div/div/div/div[2]/div[1]/main/div[3]/div/div[2]/div[2]/nav/ul/button').click()
None of these work and I'm kinda stuck. The reason I'm using aria-label for the first one is because when the next button is selected the previous button changes to the same class as the next button. Note: The button is inside a ul.
It might not work finding the element because it's not visible in UI - it is loaded but not visible, the easiest way is to move to that element and click on it.
next_button = driver.find_element_by_css_selector('[aria-label=\'Next\']')
actions = ActionChains(driver)
actions.move_to_element(next_button).perform()
next_button.click()
next_button = driver.find_element_by_xpath('//button[#class="css-1lkjxdl eanm77i0"]').click()
You was using xpath variable and finding it by css. for css selector you have to use the class (.css-1lkjxdl) and use the above code it will work and accept the answer. Thanks!!
aria-label is an attribute, not an element.
Your xpath should be fixed as follow:
button[#aria-label="Next"]
To find this button anywhere in the page, you can try:
//button[#aria-label="Next"]
Then, you can try:
button = driver.find_element_by_xpath('//button[#aria-label="Next"]')
I have this page. I have to click on Facebook icon. Upon doing it I m getting:
selenium.common.exceptions.ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with
Code is given below:
if 'log' in html.lower():
print("not logged in")
sleep(3)
#Click on Fb button
fb_element = driver.find_element_by_xpath('//a[#tooltip="Facebook"]')
fb_element.vis
fb_element.send_keys(Keys.TAB)
There is an another element with tooltip="Facebook" on the page and this element is actually invisible. Well, there are actually 10 of them:
> $x('//a[#tooltip="Facebook"]').length
10
You can find find all elements matching your locator and filter the visible one via next() and is_displayed():
facebook_links = driver.find_elements_by_xpath('//a[#tooltip="Facebook"]')
visible_link = next(link for link in facebook_links if link.is_displayed())
visible_link.click()
I have this Selenium Code that should click on a size selection button.
submit_button = driver.find_element_by_class_name('pro_sku')
elementList = submit_button.find_elements_by_tag_name("a")
elementList[3].click()
It works for other pages but now on one page I get this error:
selenium.common.exceptions.WebDriverException: Message: unknown error: Element is not clickable at point (663, 469). Other element would receive the click:
I don't understand it because I can look at the browser window that Selenium opens and I normally can click on these buttons.
How can I solve this?
Someone asked for the website. Here it is: http://de.sinobiological.com/GM-CSF-CSF2-Protein-g-19491.html
You can use Xpath for element selection and then use the following method
# Click on Element
def element_click(self, xpath):
xpath = re.sub('"', "'", xpath)
browser.execute_script("""
var elements = document.evaluate("%s",
document,
null,
XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
null);
var im = elements.snapshotItem(0);
im.click();
""" %(xpath)
)
So if your x-path is correct and item is present on DOM then definitely it will get clicked.
Happy Coding
You can use action_chains to simulate mouse movment
actions = ActionChains(driver)
actions.move_to_element(elementList[3]).perform()
elementList[3].click()
Edit
The <a> tags are not the actual sizes. Try
sizes = driver.find_elements_by_class_name('size_defaut')
sizes[3].click()
Try below:-
driver.execute_script("arguments[0].click();", elementList[3])
Hope it will help you :)
currently i'm writing a web scraper that will work in infinite loop. It gets a page, searches for some buttons and clicks one of them. But sometimes it doesn't! I save a screenshot in case of some fail and it showed me that page didnt changed after button clicked.
driver.find_element_by_xpath('//input[#name = "btn"]').click()
time.sleep(3)
I have bypassed this with a loop checking does we see that element still.
while driver.find_elements_by_xpath('//input[#name = "Submit"]') != []:
driver.find_element_by_xpath('//input[#name = "Submit"]').click()
But hope to find a root cause of this. What it could be?
I also faced a similar problem with my application. Clicking the element through action class worked for me.
Here is the sample code in Java:
WebElement webElement = driver.findElement(By.id("Your ID Here"));
Actions builder = new Actions(driver);
builder.moveToElement(webElement).click(webElement);
builder.perform();
If clicking with action class does not work, you can also try clicking element by Javascript.
WebElement webElement = driver.findElement(By.id("Your ID here"));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", webElement);