Python Selenium upload file to website by class name - python

I am trying to use Python to upload files to a website. But when I looked at the Developer Inspection Tool, there is no id, and no name. I am trying to use class name, but it does not work.
Inspect window from Selenium:
Inspect window from website:
I've tried methods like these:
upload = driver.find_element_by_class_name("icomoon icon-upload2 toolbar-button fileinput-button")
upload = driver.find_element_by_class_name("fileupload")
upload = driver.find_element_by_class_name("btnContainer").
All failed.
the error message:
NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":".btnContainer"}
(Session info: chrome=91.0.4472.77)
Then I use:
upload = driver.find_element_by_css_selector('body > div.fileupload')
upload.send_keys(r'c\user\xxx\upload\excel.xlsx')
#or
upload = driver.find_element_by_xpath("//div[#class='fileupload']")
upload.send_keys(r'c\user\xxx\upload\excel.xlsx')
The error message:
ElementNotInteractableException: Message: element not interactable
(Session info: chrome=91.0.4472.77)
Can anyone help me with this?
Thanks,
JH.

If you want a quick solution you can always try getting by ccs selctor. Right click on the element in the inspect widow. click on copy > selector. And then in python write driver.find_element_by_css_selector('paste the selector here')
This is not an elegant solution but it should work.

Related

Unable to scrape comments from YouTube in selenium

I am writing a YouTube scraper in selenium. I am unable to scrape comments.
Following is my code:
driver.get("https://www.youtube.com/watch?v=b5jt2bhSeXs")
wait = WebDriverWait(driver, 20)
time.sleep(10)
# wait.until(EC.presence_of_element_located((By.TAG_NAME, "ytd-comments")))
wait.until(EC.presence_of_element_located((By.TAG_NAME, "ytd-item-section-renderer")))
get_comments_and_commentors(driver)
def get_comments_and_commentors(driver):
scroll_down()
main_div = driver.find_element_by_id("contents")
inner = main_div.find_element_by_class_name("style-scope.ytd-item-section-renderer")
here = inner.find_element_by_tag_name("ytd-comment-renderer")
print(here)
scroll_down function is to scroll till the end to load all the comments this function is working because I can see the scrolling of page when I run this script.
The issue I am facing is that selenium is unable to find this tag "ytd-comment-renderer" instead of tag I have also tried to use the class name its giving the same error.
I am keep getting this error for both the tag and class name:
"selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"ytd-comment-renderer"}"
To fix this I tried this:
wait.until(EC.presence_of_element_located((By.TAG_NAME, "ytd-comment-renderer")))
But I was getting the TimeOutException.
What can I do to fix this error.

Need help finding the compose button in Gmail using selenium

I am having trouble with finding the compose button in Gmail. Every time I run it I get the following error message saying that the element doesn't exist
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#class="T-I T-I-KE L3"]"}
I have tried multiple things including:
ComposeButton = driver.find_element(By.XPATH, '//*[#jscontroller="eIu7Db"]')
ComposeButton = driver.find_element(By.XPATH, '//*[#class="T-I T-I-KE L3"]')
ComposeButton = driver.find_element(By.XPATH, '//*[#jsaction="click:dlrqf; clickmod:dlrqf"]')
They have all returned with the same error message
How do I access the compose button?
The greyed out part of the HTML in the linked image is for the compose button
Try this:
find_element(By.XPATH, "//div[#class='aic']/div/div")
This Worked for me
driver.find_element(By.CSS_SELECTOR, "[gh='cm']").click()
(N.B = change the word driver as per your code)

NoSuchElementException in Selenium Python

I'm trying to open a website using Selenium, find a specific search box within it and fill it with a company name (MAGAZINE LUIZA S.A. in the example) and then click the "Search" button right next to the search box, since it will not work if I just hit "enter".
nav = webdriver.Edge()
nav.implicitly_wait(10)
nav.get('http://www.b3.com.br/pt_br/produtos-e-servicos/negociacao/renda-variavel/empresas-listadas.htm')
nav.find_element_by_xpath("/html/body/form/div[3]/div[1]/div/div/div/div/div[3]/div[1]/div[1]/div[2]/div[1]/div/div[1]/label/span[1]/input[1]").send_keys('MAGAZINE LUIZA S.A.')
nav.find_elements_by_xpath('/html/body/form/div[3]/div[1]/div/div/div/div/div[3]/div[1]/div[1]/div[2]/div[1]/div/div[2]/input').click()
And I get the Xpath of the search bar and search button by inspecting them and "copying full Xpath" in Microsoft Edge.
Problem is I get this error:
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/form/div[3]/div[1]/div/div/div/div/div[3]/div[1]/div[1]/div[2]/div[1]/div/div[1]/label/span[1]/input[1]"}
(Session info: MicrosoftEdge=91.0.864.54)
But I have verified that I have copied and pasted the correct Xpaths for both the commands. Can anyone help?
You need to handle iframe, that XPath is relative to iframe.
This is your modified code
nav = webdriver.Edge()
nav.implicitly_wait(10)
nav.get('http://www.b3.com.br/pt_br/produtos-e-servicos/negociacao/renda-variavel/empresas-listadas.htm')
iframe = nav.find_element_by_xpath('/html/body/main/div[4]/div/div[1]/div[1]/div/form/section/div/div/div/iframe')
nav.switch_to.frame(iframe)
inp = nav.find_element_by_xpath("/html/body/form/div[3]/div[1]/div/div/div/div/div[3]/div[1]/div[1]/div[2]/div[1]/div/div[1]/label/span[1]/input[1]")
inp.send_keys('MAGAZINE LUIZA S.A.')
nav.find_element_by_xpath('/html/body/form/div[3]/div[1]/div/div/div/div/div[3]/div[1]/div[1]/div[2]/div[1]/div/div[2]/input').click()

Why can't I find element using class name?

I am trying to find this button and click on it.
But I get this error:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".sqdOP L3NKy y3zKF "}
(Session info: chrome=87.0.4280.88)
I have noticed that it is looking for a different string '.sqdOP L3NKy y3zKF' bc it adds this point in front of the class name. Is this the problem?
<button class="sqdOP L3NKy y3zKF " type="button">Follow</button>
Thank you!
You can also locate it with this if there isn't a button with the same class name:
driver.find_element_by_class_name("sqdOP")
Selenium doesn't recognize spaces in class names (unless you do something like what Villa_7 said) because those are actually "compound classes" (see this post and this one). It has something to do with CSS, I believe.
Fyi, it'd be helpful to see code for how you're currently trying to select the button.
If classname value contains spaces, Selenium cannot locate it via dot "."
You have to use this construction:
"[class='sqdOP L3NKy y3zKF ']"
Or just try to locate by visible text using XPath:
"//button[text()='Follow']"
If you're getting Element click intercepted exception, just try to click via JS, like this:
public void executeClickJS(WebDriver driver, WebElement webElement) {
((JavascriptExecutor) driver).executeScript("arguments[0].click();", webElement);
}

Python Selenium Webdriver: return error after returning few elements

I am trying to retrieve the information related to reviewers posted on google play store. I tried to extract the name of reviewers and rating given by reviewer. Code returns the first set of information then it returns the error. I tried to debug but couldnt fix it. Here is my code:
driver = webdriver.Chrome(driverLocation)
driver.get('https://play.google.com/store/apps/details?id=com.getsomeheadspace.android&showAllReviews=true')
driver.implicitly_wait(20)
parentElement = driver.find_element_by_xpath('//h3[contains(text(),"User reviews")]//parent::div/div[1]/div')
for child in parentElement.find_element_by_tag_name('div'):
name = child.find_element_by_class_name('X43Kjb').text
indvd_rating = child.find_element_by_xpath('//span[#class="nt2C1d"]//div[#aria-label]').get_attribute('aria-label')
print(name)
print(indvd_rating)
driver.quit()
Error is:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".X43Kjb"}
Could anyone point out where am I making the mistake?

Categories

Resources