Passing variable into xpath selenium, Python - python

This is the code that I am using trying to pass a variable into the xpath.
def findcase(year):
select = Select(driver.find_element_by_id('SheetContentPlaceHolder_coaCaseSearch_ddlCaseYear'))
select.select_by_value(str(year))
Getting the following error message:
Traceback (most recent call last):
File "/Users/jackboland/Library/Mobile Documents/com~apple~CloudDocs/Python/Selenium/8thDistBriefScraping_FUNCTIONVersion.py", line 27, in <module>
findcase('2013','100604')
File "/Users/jackboland/Library/Mobile Documents/com~apple~CloudDocs/Python/Selenium/8thDistBriefScraping_FUNCTIONVersion.py", line 18, in findcase
select = Select(driver.find_element_by_id('SheetContentPlaceHolder_coaCaseSearch_ddlCaseYear'))
File "/anaconda2/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 353, in find_element_by_id
return self.find_element(by=By.ID, value=id_)
File "/anaconda2/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 957, in find_element
'value': value})['value']
File "/anaconda2/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 314, in execute
self.error_handler.check_response(response)
File "/anaconda2/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"id","selector":"SheetContentPlaceHolder_coaCaseSearch_ddlCaseYear"}
(Session info: chrome=66.0.3359.181)
(Driver info: chromedriver=2.38.552518 (183d19265345f54ce39cbb94cf81ba5f15905011),platform=Mac OS X 10.13.4 x86_64)
I have tried the string in quotes (double and single) I have tried inserting the variable with a str(variable). Nothing doin' Cannot get the function to permit the passing in of a variable.
This is the relevant HTML. But, note that when I pass in an actual number, a year, the above code works perfectly. It only fails when trying to pass in a variable for that number.
//*[#id="SheetContentPlaceHolder_coaCaseSearch_ddlCaseYear"]

I figured this out. The code that works is below:
year = 2014
yearstr = str(year)
casenum = 100604
casenumstr = str(casenum)
select = Select(driver.find_element_by_id('SheetContentPlaceHolder_coaCaseSearch_ddlCaseYear'))
select.select_by_value(yearstr)
#inserts the case number in that box below the year
driver.find_element_by_id('SheetContentPlaceHolder_coaCaseSearch_txtCaseNum').send_keys(casenumstr)

Related

Selenium Python Invalid Column Error message

Here is the code which I use to open the new tab using Selenium Python,
self.driver.execute_script("window.open(arguments[0])", self.listing_url)
self.driver.switch_to.window(self.driver.window_handles[1])
time.sleep(10)
and it works perfectly. But I am planning to use WebDriverWait instead of time.sleep
self.driver.execute_script("window.open(arguments[0])", self.listing_url)
self.driver.switch_to.window(self.driver.window_handles[1])
WebDriverWait(self.driver, 20).until(EC.presence_of_element_located((By.XPATH, '//div[#class="honbun"]')))
and when I am using it, it's throwing an error. Here is the error log:
Traceback (most recent call last):
File "D:\tender_scraper\engine\Spider.py", line 308, in parse_index_page
index_handle = self.load_listing_page(listing, i, index_listing)
File "D:\tender_scraper\spiders\gunmapref_listing_spider.py", line 298, in load_listing_page
WebDriverWait(self.driver, 20).until(EC.presence_of_element_located((By.XPATH, '//div[#class="honbun"]')))
File "D:\scraper\lib\site-packages\selenium\webdriver\support\wait.py", line 78, in until
value = method(self._driver)
File "D:\scraper\lib\site-packages\selenium\webdriver\support\expected_conditions.py", line 64, in _predicate
return driver.find_element(*locator)
File "D:\scraper\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 1251, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "D:\scraper\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 430, in execute
self.error_handler.check_response(response)
File "D:\scraper\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 247, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: missing or invalid columnNumber
(Session info: headless chrome=104.0.5112.48)
Any idea what's wrong with it & how to solve it? Previously, I used chrome 103 and changed it to 104 but still got the same error. It seems the error only occurs when changing into the new tab.

How do I edit an online xlsx file in python?

I need to edit this file with any python module:
https://1drv.ms/x/s!Aq2lKlhSyqf5mjiC6CYkC1QGXNSy?e=Wh88AT
but I don't want to download it edit and then upload again. I tried using webbot, but I couldn't even find the 'editing mode' button.
import time
from time import sleep
from webbot import Browser
driver = Browser()
driver.new_tab(url='https://1drv.ms/x/s!Aq2lKlhSyqf5mjiC6CYkC1QGXNSy?e=Wh88AT')
driver.switch_to_tab(1)
driver.close_current_tab()
print("waiting...")
sleep(20)
print("start")
driver.click(classname = 'list-349' , tag = 'button' )
but it returns this:
Traceback (most recent call last):
File "main.py", line 19, in <module>
driver.click(classname = 'list-349' , tag = 'button' )
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/webbot/webbot.py", line 466, in click
maxElements = self.__find_element(text, tag, classname, id, number, css_selector, xpath, loose_match)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/webbot/webbot.py", line 294, in __find_element
element_fetch_helper(("//body//{}".format(tag)), score=50)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/webbot/webbot.py", line 202, in element_fetch_helper
add_to_init_text_matches_score(self.driver.find_elements_by_xpath(xpath), score)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 410, in find_elements_by_xpath
return self.find_elements(by=By.XPATH, value=xpath)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 1005, in find_elements
return self.execute(Command.FIND_ELEMENTS, {
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchWindowException: Message: no such window: target window already closed
from unknown error: web view not found
(Session info: chrome=91.0.4472.101)
(Driver info: chromedriver=2.39.562737 (dba483cee6a5f15e2e2d73df16968ab10b38a2bf),platform=Linux 5.15.0-1012-gcp x86_64)
I'm using replit by the way.

Unable to store a function in a variable in Selenium

Okay so I was writing my first selenium code which is:
browser.get('https://www.jetbrains.com/pycharm/')
browser.find_element_by_link_text('Download')
<selenium.webdriver.remote.webelement.WebElement (session="69f43bf4a5e98baf24ad3746dd01b514", element="0fa96685-d871-464f-aaaa-af02238a5f08")>
I wanted to simply go to the pycharm website and click on downloads, which worked fine.
But when I tried to store the previous function in a variable like so:
elem = browser.find_element_by_link_text('Download')
I got all these errors:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "C:\Users\admin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 428, in find_element_by_link_text
return self.find_element(by=By.LINK_TEXT, value=link_text)
File "C:\Users\admin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
'value': value})['value']
File "C:\Users\admin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\admin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchWindowException: Message: no such window: window was already closed
(Session info: chrome=87.0.4280.66)
I have no idea what they mean or what ive done wrong. Help much appreciated.
PS: I am using pycharm and selenium chromedriver if thats helpful.
No issues in this.
from selenium import webdriver
browser = webdriver.Chrome(executable_path='C:/bin/chromedriver.exe')
browser.get('https://www.jetbrains.com/pycharm/')
element = browser.find_element_by_link_text('Download')
element.click()

Can't Understand why "selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable"

"selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable"
This is the error i get for no reason at all. i mean earlier it used to work but now it isnt.
In my code there is this website : https://www.tempinbox.xyz/
i open it using the chromedriver.
what i want to do is select (#fitschool.be) from the dropdown menu of domains.
and input some variable string which i have stored as variable "str(res)"
my code is:-
second_tab = webdriver.Chrome(options=options,)
second_tab.get("https://www.tempinbox.xyz/")
randombutton = second_tab.find_element_by_xpath("/html/body/main/div[1]/div/div[1]/div[2]/form[1]/input[2]") #for the email generation
randombutton.send_keys(str(res))
mainurl = second_tab.current_url
selectoption1=second_tab.find_element_by_id("selected-domain").click()
selectoption2=second_tab.find_element_by_css_selector(".domain-selector:nth-child(3)").click()
clickfinal= second_tab.find_element_by_xpath("//input[#value='Create']").click()
emailentry = str(res)+ "#fitschool.be"
print ("your email is " + emailentry)
but it's giving me errors....
Traceback (most recent call last):
File "C:\Users\BRS\Desktop\AccountChef\wattpad acc maker.py", line 45, in <module>
selectoption2=second_tab.find_element_by_css_selector(".domain-selector:nth-child(3)").click()
File "C:\Users\BRS\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
self._execute(Command.CLICK_ELEMENT)
File "C:\Users\BRS\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
return self._parent.execute(command, params)
File "C:\Users\BRS\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\BRS\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
(Session info: chrome=84.0.4147.105)
pls suggest some changes and why this is happening. Thanks :)
Instead of the following two lines:
selectoption1=second_tab.find_element_by_id("selected-domain").click()
selectoption2=second_tab.find_element_by_css_selector(".domain-selector:nth-child(3)").click()
Try to set the innerHTML value with .execute_script:
element = second_tab.find_element_by_id("selected-domain")
target = "#fitschool.be"
second_tab.execute_script("arguments[0].innerHTML = '{}';".format(target), element)

Python Selenium-Debug a Simple Code with YouTube

I followed this tutorial for Selenium with Python:
So I did the same as the tutorial:
from selenium import webdriver
driver=webdriver.Chrome()
driver.get('https://www.youtube.com/')
searchbox = driver.find_element_by_xpath('//*[#id="search-input"]')
searchbox.send_keys('cooking')
But I get this error:
Traceback (most recent call last):
File "C:/ANGUS/DATA_ANALYSIS/WEBSCRAPING/TextXXX.py", line 7, in <module>
searchbox.send_keys('cooking')
File "C:\Users\Angus\PycharmProjects\Test\venv\lib\site-packages\selenium\webdriver\remote\webelement.py", line 477, in send_keys
self._execute(Command.SEND_KEYS_TO_ELEMENT,
File "C:\Users\Angus\PycharmProjects\Test\venv\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
return self._parent.execute(command, params)
File "C:\Users\Angus\PycharmProjects\Test\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\Angus\PycharmProjects\Test\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
(Session info: chrome=81.0.4044.138)
How can I fix this?
Another way to do that in order not to use the search is by inserting the query itself.
Code:
your_query = "cooking"
driver.get("https://www.youtube.com/results?search_query=" + str(your_query))
time.sleep(5)
In this way, it is like you are going immediately in the output that you want after searching a specific query of yours. So, let's say that instead of typing cooking, you can .get() to the URL you want by adding your query into this URL.

Categories

Resources