-2

I'm trying to skip blacklisted items when working with my Selenium Python program, but I can't figure out how to make this snippet work -

    channel = driver.find_element_by_xpath('/html/body/ytd-app/div[1]/ytd-page-manager/ytd-watch-flexy/div[3]/div[1]/div/div[7]/div[3]/ytd-video-secondary-info-renderer/div/div[2]/ytd-video-owner-renderer/a').get_attribute('aria-label')

    print(channel)   
    print(blacklistchannels[3])

    if channel in blacklistchannels:
        print('Blacklisted Channel, Skipping...')
        continue
    else:
        print('There is still a problem')

Even though when printed, the the channel name and blacklisted item (3) are exactly the same. It still follows the else, and prints out "there is still a problem"

Any help would be greatly appreciated, thanks!

Edit - The continue there is irrelevant to the snippet, it's just for continuing a loop this is in.

Solution - .Stripping() the blacklist and Channel name, to get rid of the whitespaces and /n I had for the newlines.

ENVY
  • 1
  • 1

3 Answers3

1

Pretty tough to guess the reason why else() block gets executed. Perhaps the relevant HTML would have helped us to diagnose the issue.

However I suspect value extracted by get_attribute('aria-label') contains blank spaces / white spaces.

Solution

You can use python's strip() method to remove the blank spaces / white spaces as follows:

channel = driver.find_element_by_xpath('/html/body/ytd-app/div[1]/ytd-page-manager/ytd-watch-flexy/div[3]/div[1]/div/div[7]/div[3]/ytd-video-secondary-info-renderer/div/div[2]/ytd-video-owner-renderer/a').get_attribute('aria-label').strip(' \n')
print(channel)   
print(blacklistchannels[3])
if channel in blacklistchannels:
    print('Blacklisted Channel, Skipping...')
else:
    print('There is still a problem')
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I just noticed that the 2nd print (the one from the list) has a blank line under it. This made me realise that it was probably a /n I was printing into the text file to make new lines. How would I go about stripping the entire list of /n's? – ENVY Jan 04 '19 at 15:53
  • Checkout the updated answer and let me know the status. – undetected Selenium Jan 04 '19 at 15:58
  • `blacklistchannels = [x.strip('\n') for x in blacklistchannels]` but it may be easier to strip the newline as the data is being put into the list. – tgikal Jan 04 '19 at 16:02
  • You can use `print(repr(blacklistchannels[3]))` to see what is there with no formatting done. – tgikal Jan 04 '19 at 16:10
  • @DebanjanB Thanks, figured it out on my own. There aren't any whitespaces in the aria-label, the problem is in fact the /n's inserted for the new lines from my text file. Just for looped the list and stripped them into another list, works great now. – ENVY Jan 04 '19 at 16:13
0

why not just use this?

channel = driver.find_element_by_xpath('/html/body/ytd-app/div[1]/ytd-page-manager/ytd-watch-flexy/div[3]/div[1]/div/div[7]/div[3]/ytd-video-secondary-info-renderer/div/div[2]/ytd-video-owner-renderer/a').get_attribute('aria-label')

print(channel)   
print(blacklistchannels[3])

if channel in blacklistchannels:
    print('Blacklisted Channel, Skipping...')
    # continue
else:
    print('There is still a problem')

Im not sure why you are using continue in an if statement

mikeg
  • 444
  • 3
  • 13
0

Just a side note on how to catch issues like this:

>>> a = "hello\r\n"
>>> print(a)
hello

>>> 

As you can tell, it is easy to over look the "\r\n" with a normal print statement.

This is where repr() is helpful:

>>> a = "hello\r\n"
>>> print(repr(a))
'hello\r\n'
>>> 
tgikal
  • 1,667
  • 1
  • 13
  • 27